Hasty Briefsbeta

PEP 810 – Explicit lazy imports

a day ago
  • #imports
  • #python
  • #performance
  • PEP 810 introduces explicit lazy imports in Python 3.15, allowing developers to defer module loading until first use.
  • Lazy imports reduce startup time and memory usage, particularly beneficial for CLI tools and applications with large dependency graphs.
  • The proposal maintains backward compatibility, with lazy imports enabled only where explicitly requested via the `lazy` keyword or `__lazy_modules__` attribute.
  • Lazy imports are thread-safe and support subinterpreters, with minimal performance overhead thanks to adaptive interpreter optimizations.
  • The PEP includes mechanisms for global control of lazy imports (`-X lazy_imports`, `PYTHON_LAZY_IMPORTS`) and fine-grained filtering via `sys.set_lazy_imports_filter()`.
  • Error handling is enhanced with exception chaining, showing both where the lazy import was defined and where it was first accessed.
  • The reference implementation is available on GitHub, and the PEP discusses alternative designs and rejected ideas.