Skip to content

Provide importlib.util.lazy_import helper function #76373

Closed

Description

@ncoghlan
BPO 32192
Nosy @warsaw, @brettcannon, @ncoghlan, @ericsnowcurrently, @nanjekyejoannah, @iritkatriel
PRs
  • bpo-32192: A basic lazy importer example #21330
  • 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:

    assignee = None
    closed_at = <Date 2020-10-05.18:34:50.201>
    created_at = <Date 2017-12-01.08:50:41.820>
    labels = ['3.8', 'type-feature', '3.7', 'docs']
    title = 'Provide importlib.util.lazy_import helper function'
    updated_at = <Date 2020-10-05.18:34:50.200>
    user = 'https://github.com/ncoghlan'

    bugs.python.org fields:

    activity = <Date 2020-10-05.18:34:50.200>
    actor = 'brett.cannon'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2020-10-05.18:34:50.201>
    closer = 'brett.cannon'
    components = ['Documentation']
    creation = <Date 2017-12-01.08:50:41.820>
    creator = 'ncoghlan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32192
    keywords = ['patch']
    message_count = 6.0
    messages = ['307370', '307396', '307481', '373620', '377893', '378073']
    nosy_count = 7.0
    nosy_names = ['barry', 'brett.cannon', 'ncoghlan', 'docs@python', 'eric.snow', 'nanjekyejoannah', 'iritkatriel']
    pr_nums = ['21330']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue32192'
    versions = ['Python 3.7', 'Python 3.8']

    Activity

    1. ncoghlan commented on Dec 1, 2017

      @ncoghlan
      ContributorAuthor

      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"

    2. brettcannon commented on Dec 1, 2017

      @brettcannon
      Member

      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.

    3. ncoghlan commented on Dec 3, 2017

      @ncoghlan
      ContributorAuthor

      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

    4. nanjekyejoannah commented on Jul 13, 2020

      @nanjekyejoannah
      Contributor

      New changeset 8dd32fe by Joannah Nanjekye in branch 'master':
      bpo-32192: A basic lazy importer example (GH-21330)
      8dd32fe

    5. iritkatriel commented on Oct 3, 2020

      @iritkatriel
      Member

      This seems complete. Can is be closed?

    6. brettcannon commented on Oct 5, 2020

      @brettcannon
      Member

      Yep, I think the example is enough to close this. Thanks!

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of life3.8 (EOL)end of lifedocsDocumentation in the Doc dirtype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions