New submission from Pox TheGreat <poxthegr...@gmail.com>:

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?

----------
components: Library (Lib)
files: LazyImport.zip
messages: 361705
nosy: Pox TheGreat
priority: normal
severity: normal
status: open
title: importlib: lazy loading can result in reimporting a submodule
type: behavior
versions: Python 3.7, Python 3.8
Added file: https://bugs.python.org/file48889/LazyImport.zip

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

Reply via email to