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.

Author ncoghlan
Recipients Rolf Campbell, brett.cannon, eric.snow, ncoghlan, r.david.murray
Date 2018-05-23.13:26:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1527082016.51.0.682650639539.issue33547@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2018-05-23 13:26:56ncoghlansetrecipients: + ncoghlan, brett.cannon, r.david.murray, eric.snow, Rolf Campbell
2018-05-23 13:26:56ncoghlansetmessageid: <1527082016.51.0.682650639539.issue33547@psf.upfronthosting.co.za>
2018-05-23 13:26:56ncoghlanlinkissue33547 messages
2018-05-23 13:26:56ncoghlancreate