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
Title: Provide importlib.util.lazy_import helper function
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.8, Python 3.7
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: Nick 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: Nick 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!
History
Date User Action Args
2022-04-11 14:58:55adminsetgithub: 76373
2020-10-05 18:34:50brett.cannonsetstatus: open -> closed
resolution: fixed
messages: + msg378073

stage: patch review -> resolved
2020-10-03 17:09:34iritkatrielsetnosy: + iritkatriel
messages: + msg377893
2020-07-13 21:31:14nanjekyejoannahsetmessages: + msg373620
2020-07-05 04:07:13nanjekyejoannahsetkeywords: + patch
nosy: + nanjekyejoannah

pull_requests: + pull_request20480
stage: needs patch -> patch review
2017-12-03 01:29:02ncoghlansetnosy: + docs@python
messages: + msg307481

assignee: docs@python
components: + Documentation
2017-12-01 17:32:29brett.cannonsetmessages: + msg307396
2017-12-01 15:28:44barrysetnosy: + barry
2017-12-01 08:51:04ncoghlansetnosy: + brett.cannon, eric.snow
stage: needs patch
type: enhancement

versions: + Python 3.7, Python 3.8
2017-12-01 08:50:41ncoghlancreate