Issue 8389: Incomprehensible import error (original) (raw)
I ran into an ImportError I was unable to explain:
$ python -c "import a.b" Traceback (most recent call last): File "", line 1, in File "a/b/init.py", line 1, in import a.b.c File "a/b/c.py", line 2, in import a.b.t as t AttributeError: 'module' object has no attribute 'b'
This is the source code:
$ tail find -name \*.py
==> ./demo.py <==
import a.b
==> ./a/init.py <==
==> ./a/b/c.py <==
Does not work:
import a.b.t as t
Works:
import a.b
from a.b import t
==> ./a/b/t.py <==
==> ./a/b/init.py <== import a.b.c
Works:
import a.b.t
Replacing any import with one of the versions annotated as working fixes it. Stripping another level from the package tree fixes it as well. Why!?
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.
First off, the 'as' clause is not influencing anything; simply try to assign t = a.b.t
and you still get the error.
Second, you are trying to reference a.b in a.b.c through your a.b.t import before a.b has finished importing. This is causing the import system to not have a chance to bind the b module to a.b before you try to reference it.
The reason from a.b import t
works is the 'from' clause is handled after all imports resolve as a separate step, so everything settles, gets imported, and then Python tries to get a.b.t long after a.b if finished.
In other words it's a funky import cycle which you break with from a.b import t
. Nothing we can do to change it.