Python's Lazy Imports: Why It Took Three Years and Two Attempts
3 days ago
- #Python
- #Performance
- #Programming
- Python 3.15 introduces a 'lazy' keyword for imports to improve startup time and reduce memory usage.
- Companies like Meta and Hudson River Trading (HRT) implemented lazy imports in their Python forks (Cinder and HRT's fork) due to urgent performance needs.
- PEP 690 proposed lazy imports with a global flag but was rejected due to concerns about ecosystem fragmentation and implementation complexity.
- PEP 810, the revised proposal, introduced explicit 'lazy import' syntax, addressing prior concerns and gaining Steering Council approval.
- Lazy imports defer module loading until first use, significantly speeding up CLI tools by avoiding unnecessary imports.
- PEP 810 avoids modifying Python's core dict internals, using lightweight proxies for deferred imports instead.
- Wildcard imports (from foo import *) cannot be lazy, and import errors are deferred until first use.
- The three-year process involved real-world evidence from large codebases, ensuring the design was robust and practical.
- PEP 810 was unanimously accepted by the Python Steering Council in November 2025, with Python 3.15 slated to include the feature in October 2026.