importing twice triggers load
Opened this issue · 3 comments
Problem
When importing a module setup with apipkg, successive imports, even if in different files, trigger a full load. I thought it would only load when actually used. I have some anecdotal observation that the behavior is intermittent, but when I put together this test code, it reproduces every time
How to Reproduce
system info:
MacOS 14.5
Python 3.12.4
Consider the following filetree
mypkg/
- module.py
- lazy.py
with contents:
# lazy.py
import apipkg
apipkg.initpkg(__name__,{
"module": "mypkg.module",
})
# module.py
print("module loaded")
then in a terminal:
> python
>>> import mypkg.lazy.module
>>> import mypkg.lazy.module
module loaded
i believe this relates to import looking up the attributes as requested, based on the structure of the import request i'd consider it the bug that it was not loaded the first time
the request in import was for mypkg.lazy.module
- at that point it needs to resolve
i suspect theres a shortcut in python, but the actual bug is not loading it the first time
never ever request by name something thats nested in a lazy module if you dont want it included
I see. Perhaps I misunderstood the operation then. This means that only the top-level lazy module specification is meant to lie dormant (i.e. lazy) and any access below the top level is meant to trigger a full load
Accesses do not trigger full load, but import has to validate the existence of the full imported name
So lazy
would only ensure the existence of the lazy module
However lazy.foo
needs to raise a import error if foo doesn't exist