Skip to content

Improve import time of various stdlib modules #118761

Description

@layday

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

Activity

  1. hugovk commented on Aug 5, 2024

    @hugovk
    Member

    @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.

  2. layday commented on Aug 5, 2024

    @layday
    Author

    Sure!

  3. danielhollas commented on Aug 6, 2024

    @danielhollas
    Contributor

    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.

  4. changed the title [-]Further improve import time of `importlib.metadata`[/-] [+]Improve import time of various stdlib modules[/+] on Aug 6, 2024
  5. added a commit that references this issue on Aug 7, 2024
  6. added a commit that references this issue on Aug 22, 2024
  7. danielhollas commented on Aug 27, 2024

    @danielhollas
    Contributor

    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.

  8. picnixz commented on Aug 31, 2024

    @picnixz
    Member

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

  9. added a commit that references this issue on Sep 1, 2024
  10. added a commit that references this issue on Nov 21, 2024
  11. 110 remaining items

  12. added a commit that references this issue on Apr 7, 2025
  13. added a commit that references this issue on Apr 8, 2025
  14. added 2 commits that reference this issue on Apr 16, 2025
  15. JelleZijlstra commented on Apr 17, 2025

    @JelleZijlstra
    Member

    #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.

  16. added a commit that references this issue on Apr 24, 2025
  17. danielhollas commented on May 5, 2025

    @danielhollas
    Contributor

    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

  18. added a commit that references this issue on May 5, 2025
  19. added a commit that references this issue on Jul 12, 2025
  20. hugovk commented on Aug 15, 2025

    @hugovk
    Member

    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.

  21. ofek commented on Aug 16, 2025

    @ofek
    SponsorContributor
  22. added a commit that references this issue on Mar 25, 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 fixesperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions