[issue43477] from x import * behavior inconsistent between module types.

Brett Cannon report at bugs.python.org
Tue Mar 16 15:51:34 EDT 2021


Brett Cannon <brett at python.org> added the comment:

Thanks for the clarification! I think I understand what's going on now, and the logic is actually expected.

When you do `from .test_submodule import *`, Python must first import `test_pkg.test_submodule` in order to get you the object for the `import *` part (or frankly anything that comes after `import`). As part of importing `test_pkg.test_submodule`, it automatically gets attached to `test_pkg`, otherwise we wouldn't be able to cache the module in `sys.modules` and prevent redundant/duplicate imports.

As such, when you do `import test_pkg` in`test.py`, the fact that `test_pkg.__init__` has to import `test_pkg.test_submodule` means `test_pkg will automatically end up with a `test_submodule` attribute. That's why your `print()` function call succeeds.

If I'm still misunderstanding, can you please use an `assert` statement that fails because the logic doesn't work the way you expect it to be?

----------
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43477>
_______________________________________


More information about the Python-bugs-list mailing list