Issue 32192: Provide importlib.util.lazy_import helper function - Python tracker

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.

classification
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: barry, brett.cannon, docs@python, eric.snow, iritkatriel, nanjekyejoannah, ncoghlan
Priority: normal Keywords: patch

Created on 2017-12-01 08:50 by ncoghlan, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21330 merged nanjekyejoannah, 2020-07-05 04:07
Messages (6)
msg307370 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-12-01 08:50
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"
msg307396 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-12-01 17:32
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.
msg307481 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2017-12-03 01:29
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
msg373620 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2020-07-13 21:31
New changeset 8dd32fe645c9503cf8e6be4b1580c3a59b450168 by Joannah Nanjekye in branch 'master':
bpo-32192: A basic lazy importer example (GH-21330)
https://github.com/python/cpython/commit/8dd32fe645c9503cf8e6be4b1580c3a59b450168
msg377893 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-10-03 17:09
This seems complete. Can is be closed?
msg378073 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-10-05 18:34
Yep, I think the example is enough to close this. Thanks!