Lazy Import
A language-level syntax feature introduced in Python 3.15 that defers module imports until their first actual use via the lazy keyword, balancing code cleanliness with startup performance.
Timeline (last 90 days)
Migrating to lazy imports is not zero-cost: developers need to check whether modules have import-time side effects, confirm that type checking and IDE completion still work properly, and assess whether the latency on first access is acceptable.
Lazy imports defer a module's actual loading until its first attribute access.
The cost of lazy imports is that every attribute access must go through a layer of indirection, and the actual timing of a module's loading is no longer intuitive.
The speedup of the CLI tool comes mainly from reducing the amount of top-level code executed at startup and avoiding cascading imports.
The 4x speedup is a result on a specific tool and cannot be directly extrapolated to all projects; the magnitude of the speedup depends on the size of the module graph and the proportion of imports skipped at startup.
When a tool needs almost all modules immediately after startup, lazy imports may merely shift the cost, and total elapsed time may not decrease, or may even increase slightly due to the extra indirection layer.
For code that relies on import side effects (e.g., registering plugins, modifying global state), making imports lazy may change execution order and introduce hard-to-detect bugs.
If the performance bottleneck is not in the import phase but in I/O, network requests, or computationally intensive logic, lazy imports cannot address the root problem.
Before migrating to lazy imports, one should first do performance profiling to confirm that imports are indeed the hotspot, and then consider migrating.
Python 3.15 includes the explicit lazy imports feature from PEP 810, which allows module imports to be deferred using the lazy import syntax.
All Facts (10)
Migrating to lazy imports is not zero-cost: developers need to check whether modules have import-time side effects, confirm that type checking and IDE completion still work properly, and assess whether the latency on first access is acceptable.
50%UnverifiedLazy imports defer a module's actual loading until its first attribute access.
50%UnverifiedThe cost of lazy imports is that every attribute access must go through a layer of indirection, and the actual timing of a module's loading is no longer intuitive.
50%UnverifiedThe speedup of the CLI tool comes mainly from reducing the amount of top-level code executed at startup and avoiding cascading imports.
50%UnverifiedThe 4x speedup is a result on a specific tool and cannot be directly extrapolated to all projects; the magnitude of the speedup depends on the size of the module graph and the proportion of imports skipped at startup.
50%UnverifiedWhen a tool needs almost all modules immediately after startup, lazy imports may merely shift the cost, and total elapsed time may not decrease, or may even increase slightly due to the extra indirection layer.
50%UnverifiedFor code that relies on import side effects (e.g., registering plugins, modifying global state), making imports lazy may change execution order and introduce hard-to-detect bugs.
50%UnverifiedIf the performance bottleneck is not in the import phase but in I/O, network requests, or computationally intensive logic, lazy imports cannot address the root problem.
50%UnverifiedBefore migrating to lazy imports, one should first do performance profiling to confirm that imports are indeed the hotspot, and then consider migrating.
50%UnverifiedPython 3.15 includes the explicit lazy imports feature from PEP 810, which allows module imports to be deferred using the lazy import syntax.
50%