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 Pox TheGreat
Recipients Pox TheGreat
Date 2020-02-10.18:19:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1581358790.35.0.810808142023.issue39602@roundup.psfhosted.org>
In-reply-to
Content
Using the LazyLoader class one can modify the sys.meta_path finders so that every import mechanism becomes lazy. This method has been used in Mercurial and by Facebook.

My problem is that if I have a package (pa) which imports a submodule (a) in the __init__.py and accesses its attributes (or uses a from list) then that submodule is imported (executed) twice without any warning.

I traced back the problem to importlib._bootstrap.py / _find_and_load_unlocked. There is a check there if the submodule has already been imported by the parent package, but the submodule will be imported just after that check because of the _LazyModule and the __path__ attribute access of the submodule.

# Crazy side-effects!
if name in sys.modules:
	return sys.modules[name]
parent_module = sys.modules[parent]
try:
	path = parent_module.__path__


Maybe we could check if name in sys.modules after the __path__ attribute access?
History
Date User Action Args
2020-02-10 18:19:50Pox TheGreatsetrecipients: + Pox TheGreat
2020-02-10 18:19:50Pox TheGreatsetmessageid: <1581358790.35.0.810808142023.issue39602@roundup.psfhosted.org>
2020-02-10 18:19:50Pox TheGreatlinkissue39602 messages
2020-02-10 18:19:49Pox TheGreatcreate