[ python-Bugs-966431 ] import x.y inside of module x.y

SourceForge.net noreply at sourceforge.net
Sat Jun 5 19:18:06 EDT 2004


Bugs item #966431, was opened at 2004-06-04 19:58
Message generated for change (Comment added) made by jiwon
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=966431&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Armin Rigo (arigo)
Assigned to: Nobody/Anonymous (nobody)
Summary: import x.y inside of module x.y

Initial Comment:
To get to the module object from the body of the module itself, the usual trick is to import it from itself, as in:

x.py:
import x
do_stuff_with(x)

This fails strangely if x is in a package:

package/x.py:
import package.x
do_stuff_with(package.x)

The last line triggers an AttributeError: 'module' object has no attribute 'x'.  In other words, the import succeeds but the expression 'package.x' still isn't valid after it.

----------------------------------------------------------------------

Comment By: Jiwon Seo (jiwon)
Date: 2004-06-06 08:18

Message:
Logged In: YES 
user_id=595483

The error seems to be due to the calling sequence of
add_submodule and loadmodule in import.c:import_submodule.
If load_module(..) is called after add_submodule(...) gets
called, the above does not trigger Attribute Error.
I made a patch that does it, but there is a problem... 
Currently, when import produces errors, sys.modules have the
damaged module, but the patch does not. (That's why it
cannot pass the  test_pkgimport.py unittest, I think.) 
Someone who knows more about import.c could fix the patch to
behave like that.

The patch is in
http://seojiwon.dnip.net:8000/~jiwon/tmp/import.diff

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=966431&group_id=5470



More information about the Python-bugs-list mailing list