[issue24949] Identifier lookup in a multi-level package is flakey

Martin Panter report at bugs.python.org
Fri Aug 28 05:11:48 CEST 2015


Martin Panter added the comment:

It sounds like you are trying to do a plain “import email”, and then expect all the submodules under that package to work, including “email.mime.nonmultipart”. That is not how it works. I understand the submodules and subpackages are only initialized if you ask for them explicitly. Hence the “no attribute 'mime' ” error in 3.4.

In your case perhaps you should change or extend the import statement to do

import email.mime.nonmultipart

The reason why your “from . . . import” works is it also initializes the submodule that you want, and stores it in a chain of attributes of the top-level module as a side effect.

Quickly scanning through <https://docs.python.org/3/tutorial/modules.html#packages>, <https://docs.python.org/3/reference/import.html#packages>, <https://docs.python.org/3/reference/simple_stmts.html#import>, I didn’t see anything obvious pointing out that importing a top-level package doesn’t completely initialize the whole thing. But maybe there is something already there. You might have to read about how the __init__.py internals work to pick it up.

----------
nosy: +martin.panter

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


More information about the Python-bugs-list mailing list