[issue18145] Strange behavior when importing internal modules in the __init__.py of a submodule

Brett Cannon report at bugs.python.org
Wed Jun 5 22:11:43 CEST 2013


Brett Cannon added the comment:

It's because you have a nested circular import. When you import package2.subpackage from within package2.subpackage you're in package2 importing package2 and also in package2.subpackage importing package2.subpackage.

You can solve your problem by doing either ``from package2.subpackage import foo`` for ``from . import foo`` as that lets package2.subpackage be imported entirely on its own before attempting package2.subpackage.foo and thus letting the circular loop unroll and have the right attributes set since the attributes of a module for a package are set after the import completes.

Might be annoying, but tweaking this would probably break code if changed as it's very old semantics to set the attribute of a module on a package after other imports complete. This is also not a problem as long as you don't do this in an __init__ (e.g. importing package2.subpackage.bar from package2.subpackage.foo is not a problem).

----------
nosy: +brett.cannon
resolution:  -> wont fix
status: open -> closed

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


More information about the Python-bugs-list mailing list