Nick Coghlan <ncogh...@gmail.com> added the comment:

Yes, while weird, that's expected behaviour.

Rather than being due to absolute vs relative imports, the difference arises 
from the fact that in "import pkg.module", the request is explicitly for a 
submodule, so the submodule import always happens, whereas if you write "from 
func import attr", the child module import is only attempted if "func.attr" 
fails to resolve after "func" is imported.

$ echo "print(__name__)" > pkg/__init__.py
$ echo "print(__name__)" > pkg/submodule.py

$ python3 -c "import pkg; pkg.submodule = 1; import pkg.submodule; 
print(pkg.submodule)"
pkg
pkg.submodule
<module 'pkg.submodule' from '/home/ncoghlan/devel/misc/_play/pkg/submodule.py'>

$ python3 -c "import pkg; pkg.submodule = 1; from pkg import submodule; 
print(pkg.submodule)"
pkg
1

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33547>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to