scientific-python / lazy-loader

Populate library namespace without incurring immediate import costs
BSD 3-Clause "New" or "Revised" License
118 stars 19 forks source link

load triggered in IPython upon any error raised? #56

Closed dschult closed 4 months ago

dschult commented 1 year ago

While trying to find a way to make lazy_loader.load work for subpackages (see #55), I discovered that IPython triggers a load of a lazily loaded package upon any exception being raised. A regular Python session does not.

Example:

import lazy_loader
mpl = lazy_loader.load("matplotlib")
print(type(mpl))  # result is <class 'importlib.util._LazyModule'>
raise Exception()
print(type(mpl))  # result is <class 'module'>

The same code run in a python session does not trigger the loading of the lazy module.

I checked that nothing is calling the module's __getattr__ during that process. But something in IPython seems to be triggering the loading of lazily loaded modules.

Is this another reason to remove load from the package?

stefanv commented 5 months ago

@dschult I don't know; is this behavior a problem?

stefanv commented 4 months ago

Python does all sorts of introspection into objects, so it's not surprising that, somewhere along the line, it triggers importing. I will close for now, unless you think we should fix this in lazy_loader itself.