GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve import time of various stdlib modules · Issue #118761 · python/cpython

Improve import time of various stdlib modules #118761

New issue
Copy link
New issue
Copy link
Closed
Closed
Improve import time of various stdlib modules#118761
Copy link
Labels
3.14bugs and security fixesbugs and security fixesperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-importlibtype-featureA feature request or enhancementA feature request or enhancement

Description

@layday
layday
opened on May 8, 2024
Issue body actions

Feature or enhancement

Proposal:

Following on from #109653, further improvements can be made to import times.

Links to previous discussion of this feature:

https://discuss.python.org/t/deferred-computation-evalution-for-toplevels-imports-and-dataclasses/34173

For example:

importlib.metadata is often used for tasks that need to happen at import, e.g. to enumerate/load entry point plug-ins, so it might be worth seeing if we can cut down its own import time a bit more.

importlib.metadata imports zipfile at the top for a function that won't be called in the vast majority of cases. It also imports importlib.abc, which in turn imports importlib.resources, to subclass an ABC with a single, non-abstract method - I assume redefining the method in importlib.metadata would be harmless. Some other less frequently-used imports which are only accessed once or twice, such as json, could also be tucked away in their calling functions.

Linked PRs

  • gh-118761: Improve import time of pprint #122725
  • gh-118761: Speedup pathlib import by deferring shutil #123520
  • gh-121423: Improve import time of socket by writing socket.errorTab as a constant and lazy import modules #121424
  • gh-118761: Improve import time of mimetypes #126979
  • gh-118761: improve import time for pickle #128732
  • gh-118761: substitute re import in base64.b16decode for a more efficient alternative #128736
  • gh-118761: improve import time for secrets #128738
  • gh-118761: Improve import time for csv #128858
  • gh-118761: Reduce import time of gettext.py by delaying re import #128898
  • gh-118761: improve optparse import time by delaying textwrap import #128899
  • gh-118761: Improve import time of tomllib #128907
  • gh-118761: Improve import time for pstats and zipfile by removing imports to typing #128981
  • gh-118761: Improve import time of sqlite3 #129118
  • gh-118761: Improve import time of subprocess #129427
  • gh-118761: Always lazy import warnings in threading #129428
  • [3.13] gh-118761: Improve import time of subprocess (GH-129427) #129447
  • [3.12] gh-118761: Improve import time of subprocess (GH-129427) #129448
  • gh-118761: Improve import time by lazy import of warnings #129765
  • gh-118761: Improve import time by lazy import of traceback #129811
  • gh-118761: Always lazy import re in locale #129860
  • gh-118761: Improve import time of dataclasses #129925
  • gh-118761: Improve import time of cmd module #130056
  • gh-118761: Revert "Improve import time of subprocess (GH-129427)" #130201
  • [3.13] gh-118761: Revert "Improve import time of subprocess (GH-129427)" (GH-130201) #130204
  • [3.12] gh-118761: Revert "Improve import time of subprocess (GH-129427)" (GH-130201) #130205
  • gh-137855: Improve import time of sqlite3 #131796
  • gh-118761: Optimise import time for ast #131953
  • gh-137855: Optimise import time for textwrap #131956
  • gh-118761: Fix star-import of ast #132024
  • gh-118761: Fix star-import of ast (alternative) #132025
  • gh-118761: Improve import time of annotationlib #132028
  • gh-118761: Use enum._simple_enum for annotationlib.Format #132031
  • gh-118761: Cover the import time optimisations in What's New #132035
  • gh-118761: Optimise import time for shlex #132036
  • gh-118761: Optimise import time for string #132037
  • gh-137855: email.quoprimime removing re import #132046
  • gh-118761: Defer import of functools in annotationlib #132059
  • gh-118761: Lazily import annotationlib in typing #132060
  • GH-118761: Expose more core interpreter types in _types #132103
  • gh-118761: Add helper to ensure that lazy imports are actually lazy #132614
  • gh-118761: Add test_lazy_import for more modules #133057
Reacted by Daniel Hollas, Hugo van Kemenade, Cody Maloney, Wulian233, Brett Holman, Taneli Hukkinen and Soberia
Reacted by EDM115

Activity

Next
  1. AlexWaygood
    added
    topic-importlib
    performancePerformance or resource usagePerformance or resource usage
    type-featureA feature request or enhancementA feature request or enhancement
    on May 8, 2024
  2. hugovk commented on Aug 5, 2024

    @hugovk
    hugovk
    on Aug 5, 2024
    Member
    More actions

    @layday Is it okay if I repurpose this issue as an "Improve import time of various stdlib modules" like #109653 but for 3.14?

    I've got some pprint improvements, and if we have importlib.metadata and some others, we can group them under the same umbrella issue like last time.

    Reacted by Ofek Lev
  3. layday commented on Aug 5, 2024

    @layday
    layday
    on Aug 5, 2024
    Author
    More actions

    Sure!

    Reacted by Ofek Lev
  4. danielhollas commented on Aug 6, 2024

    @danielhollas
    danielhollas
    on Aug 6, 2024
    Contributor
    More actions

    I've opened a PR over at the importlib_metadata repo that avoids importing inspect. python/importlib_metadata#499

    importlib.metadata imports zipfile at the top for a function that won't be called in the vast majority of cases.
    Some other less frequently-used imports which are only accessed once or twice, such as json, could also be tucked away in their calling functions.

    @layday were you planning on tackling these?

    It also imports importlib.abc, which in turn imports importlib.resources, to subclass an ABC with a single, non-abstract method

    This seems to be solved on main, importlib.abc no longer imports importlib.resources.

  5. hugovk
    changed the title [-]Further improve import time of `importlib.metadata`[/-] [+]Improve import time of various stdlib modules[/+] on Aug 6, 2024
  6. hugovk
    added
    3.14bugs and security fixesbugs and security fixes
    on Aug 6, 2024
  7. hugovk
    added a commit that references this issue on Aug 7, 2024

    gh-118761: Improve import time of `pprint` (#122725)

    42d9bec
  8. blhsing
    added a commit that references this issue on Aug 22, 2024

    pythongh-118761: Improve import time of `pprint` (python#122725)

    0549dc7
  9. danielhollas commented on Aug 27, 2024

    @danielhollas
    danielhollas
    on Aug 27, 2024
    Contributor
    More actions

    I've opened a PR over at the importlib_metadata repo that avoids importing inspect. python/importlib_metadata#499

    This has been merged and released in version 8.4 of importlib_metadata 🎉

    importlib.metadata imports zipfile at the top for a function that won't be called in the vast majority of cases. It also imports importlib.abc, which in turn imports importlib.resources, to subclass an ABC with a single, non-abstract method - I assume redefining the method in importlib.metadata would be harmless. Some other less frequently-used imports which are only accessed once or twice, such as json, could also be tucked away in their calling functions.

    I've submitted python/importlib_metadata#502 that defers zip import, and python/importlib_metadata#503 which defers json and platform.

    Reacted by Hugo van Kemenade
  10. picnixz
    removed
    3.14bugs and security fixesbugs and security fixes
    on Aug 31, 2024
  11. picnixz commented on Aug 31, 2024

    @picnixz
    picnixz
    on Aug 31, 2024
    Member
    More actions

    (removing the 3.14 label since features always target the main branch)

  12. barneygale
    added a commit that references this issue on Sep 1, 2024

    gh-118761: Speedup pathlib import by deferring shutil (#123520)

    2304774
  13. hugovk
    added a commit that references this issue on Nov 21, 2024

    gh-118761: Improve import time of `mimetypes` (#126979)

    dc7a2b6
  14. 110 remaining items

  15. algitbot
    added a commit that references this issue on Apr 7, 2025

    main/python3: fix lazy importing in subprocess module

    ac1a729
  16. hugovk
    added a commit that references this issue on Apr 7, 2025

    gh-118761: Cover the import time optimisations in What's New (#132035)

    f0dcb29
  17. AA-Turner
    added a commit that references this issue on Apr 8, 2025

    gh-118761: Optimise import time for ``string`` (#132037)

    ee36572
  18. JelleZijlstra
    added 2 commits that reference this issue on Apr 16, 2025

    pythongh-118761: Add helper to ensure that lazy imports are actually …

    a55b37e

    gh-118761: Add helper to ensure that lazy imports are actually lazy (#…

    39ee468
  19. JelleZijlstra commented on Apr 17, 2025

    @JelleZijlstra
    JelleZijlstra
    on Apr 17, 2025
    Member
    More actions

    #132614 adds a test helper test.support.import_helper.ensure_lazy_imports that can be used to assert that when one module is imported, it doesn't import specific other modules as a side effect. This makes sure we don't accidentally regress if we've added a lazy import somewhere.

    If anyone reading this is interested, it'd be helpful to add test cases using this new helper to other modules that we've previously optimized by deferring imports.

    Reacted by Ofek Lev, Cody Maloney, Hugo van Kemenade, Michael H, Jakub Kuczys and Daniel Hollas
  20. AA-Turner
    added a commit that references this issue on Apr 24, 2025

    gh-118761: Optimise import time for ``shlex`` (#132036)

    06a26fd
  21. danielhollas commented on May 5, 2025

    @danielhollas
    danielhollas
    on May 5, 2025
    Contributor
    More actions

    If anyone reading this is interested, it'd be helpful to add test cases using this new helper to other modules that we've previously optimized by deferring imports.

    I've submitted PR for this in #133057

  22. JelleZijlstra
    added a commit that references this issue on May 5, 2025

    gh-118761: Add test_lazy_import for more modules (#133057)

    cae660d
  23. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-118761: Add test_lazy_import for more modules (python#133057)

    2388b77
  24. hugovk commented on Aug 15, 2025

    @hugovk
    hugovk
    on Aug 15, 2025
    Member
    More actions

    Python 3.14 is now in release candidate (please test!), so no more of these are allowed in 3.14. Thanks everyone for work here, some 22 modules have been improved!

    Would someone like to open a new issue for 3.15? The pending #131796 and #132046 PRs and new improvements can be covered by that.

    And we can also backport ensure_lazy_imports tests for the 3.14 improvements, to help guard against regressions.

    Reacted by Daniel Hollas and layday
  25. hugovk
    closed this as completedon Aug 15, 2025
  26. ofek commented on Aug 16, 2025

    @ofek
    ofek
    on Aug 16, 2025
    SponsorContributor
    More actions

    #137855

    Reacted by Hugo van Kemenade
  27. bonzini
    added a commit that references this issue on Mar 25, 2026

    pythongh-146385: Switch back to re to detect shlex.quote slow path

    3de138e
  28. hugovk
    added
    3.14bugs and security fixesbugs and security fixes
    on Sep 8, 2026
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.14bugs and security fixesbugs and security fixesperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-importlibtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.