-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide importlib.util.lazy_import helper function #76373
Comments
While importlib provides all the *pieces* to implement lazy imports, we don't actually provide a clear way of chaining them together into a lazy import operation. Without any error checking, that looks like: import sys
import importlib.util
def lazy_import(name):
spec = importlib.util.find_spec(name)
loader = importlib.util.LazyLoader(spec.loader)
spec.loader = loader
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
loader.exec_module(module)
return module >>> lazy_typing = lazy_import("typing")
>>> lazy_typing.TYPE_CHECKING
False I'm thinking it may make sense to just provide a robust implementation of that, and accept that it may lead to some bug reports that are closed with "You need to fix the module you're loading to be compatible with lazy imports" |
Already planning this as a PyPI package (at least to start). See https://notebooks.azure.com/Brett/libraries/di2Btqj7zSI/html/Lazy%20importing.ipynb for the design. |
Maintaining the actual implementation as a third party module seems like a good idea to me, so I'm marking this as a documentation issue instead. The idea would be to add this as an example of a very basic lazy importer under https://docs.python.org/3/library/importlib.html#examples |
This seems complete. Can is be closed? |
Yep, I think the example is enough to close this. Thanks! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: