This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Incomprehensible import error
Type: Stage:
Components: Interpreter Core Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, torsten, ysj.ray
Priority: normal Keywords:

Created on 2010-04-13 23:19 by torsten, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
import_error.tar.gz torsten, 2010-04-13 23:19 tar archive of the example
Messages (4)
msg103091 - (view) Author: Torsten Landschoff (torsten) * Date: 2010-04-13 23:19
I ran into an ImportError I was unable to explain:

$ python -c "import a.b"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "a/b/__init__.py", line 1, in <module>
    import a.b.c
  File "a/b/c.py", line 2, in <module>
    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!?
msg103092 - (view) Author: Torsten Landschoff (torsten) * Date: 2010-04-13 23:21
Err, typo, in c.py I meant to write

# Does not work:
import a.b.t as t
# Works:
# import a.b.t

So without renaming it it works.
msg103123 - (view) Author: ysj.ray (ysj.ray) Date: 2010-04-14 12:45
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.
msg103144 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2010-04-14 20:15
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.
History
Date User Action Args
2022-04-11 14:56:59adminsetgithub: 52636
2010-04-14 20:15:08brett.cannonsetstatus: open -> closed
resolution: not a bug
messages: + msg103144
2010-04-14 12:45:52ysj.raysetnosy: + ysj.ray
messages: + msg103123
2010-04-14 11:31:19r.david.murraysetnosy: + brett.cannon
2010-04-13 23:26:36benjamin.petersonsettitle: Incomprehensibly import error -> Incomprehensible import error
2010-04-13 23:21:05torstensetmessages: + msg103092
2010-04-13 23:19:16torstencreate