GeezerVison is showing github.com — Improve import time of various stdlib modules · Issue #118761 · python/cpython
Improve import time of various stdlib modules #118761DescriptionFeature 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
Reacted by Daniel Hollas, Hugo van Kemenade, Cody Maloney, Wulian233, Brett Holman, Taneli Hukkinen and Soberia Reacted by EDM115 Activityhugovk commented on Aug 5, 2024 @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. layday commented on Aug 5, 2024 danielhollas commented on Aug 6, 2024 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. changed the title [-]Further improve import time of `importlib.metadata`[/-] [+]Improve import time of various stdlib modules[/+] on Aug 6, 2024danielhollas commented on Aug 27, 2024
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 picnixz commented on Aug 31, 2024 (removing the 3.14 label since features always target the main branch) JelleZijlstra commented on Apr 17, 2025 #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 danielhollas commented on May 5, 2025
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 hugovk commented on Aug 15, 2025 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 ofek commented on Aug 16, 2025 SponsorContributor More actions Reacted by Hugo van Kemenade
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|