[issue8389] Incomprehensible import error

Ray.Allen report at bugs.python.org
Wed Apr 14 14:45:55 CEST 2010


Ray.Allen <ysj.ray at gmail.com> added the comment:

The 'as' trigger some more instructions after import, not just renaming the loaded file name, to be specific in your example is, load attribute 'b' from 'a', and then load attribute 'c' from 'b'.

'import a.b.t' do import a.b.t and then just store 'a' in local namespace which is a reference of module 'a'
while, 
'import a.b.t as t' do import a.b.t and then load attribute 'b' from module 'a', then load attribute 't' from module 'b', finally store 't' in local namespace as a reference of a.b.t

But at that time('import a.b.t as t'), the statement 'import a.b' in demo.py hasn't finished executing yet, (because it triggers the statement 'import a.b.c' in a/b/__init__.py, which then triggers the statement 'import a.b.t as t' in a/b/c.py), along with the a/b/__init.py__.py file, although the module a has been imported, but module 'b' hasn't finished importing, it't only put in sys.modules, its module's code hasn't finishing executing(the code in a/b/__init__.py). In this case the module 'b' is considered not finishing importing, so it   hasn't been put in module a's dict as an attribute yet. 

So when the statement 'import a.b.t as t' executes in a/b/c.py, the module 'a' hasn't the attribute 'b'. But after the statement 'import a.b' in demo.py, the a/b/__init__.py file has complete executing, and the module b has finished importing, module 'b' has been put in module a's dict, at this time, 'load attribute b from a' is valid. So the import a.b as b in demo.py also works.

----------
nosy: +ysj.ray

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8389>
_______________________________________


More information about the Python-bugs-list mailing list