Source linked

Python 3.15's Lazy Imports Skip Pandas Until You Need It

blog.jetbrains.com@vibrant_tiger4 hours ago·Developer Tools·3 comments

By marking imports with the new `lazy` keyword, Python 3.15 defers module loading to first use, cutting startup time in half for a typical pandas-heavy script.

pythonpep 810jetbrainspycharmlazy importsperformance

Half the startup time in a typical data science script goes to importing pandas even when it's never used. Python 3.15's explicit lazy imports fix that with a single keyword change.

How PEP 810 Defers Module Loading

PEP 810 introduces lazy import syntax that tells CPython to defer finding and executing a module until the first time you access one of its attributes. Instead of loading the whole module into sys.modules eagerly, the interpreter creates a types.LazyImportType proxy object and registers the module name in sys.lazy_modules. The proxy gets replaced ("reified") with the real module only when code actually touches it. If reification fails, the error shows both where the lazy import was defined and where it was triggered.

Benchmarking with PyCharm and tuna

JetBrains engineers ran a typical script that imports pandas, matplotlib, and numpy but only uses numpy and matplotlib. Using the -X importtime flag and the tuna profiler, they saw the eager import spent nearly half its total time on pandas alone. After switching all three imports to lazy import pandas as pd, lazy import matplotlib.pyplot as plt, and lazy import numpy as np, the pandas import line vanished from the profile entirely. Total startup time dropped proportionally.

You can reproduce this today with Python 3.15.0b1 installed via uv or pyenv. PyCharm's syntax highlighting doesn't yet recognize the lazy keyword, but the interpreter handles it correctly. Use pdb to inspect sys.lazy_modules and watch modules move to sys.modules only after they are first used.

Inside the Proxy Object Mechanism

When you write lazy import foo, CPython calls __lazy_import__ instead of __import__. The resulting proxy lives in the importing module's namespace. The actual import happens at access time: the proxy calls __import__, adds the module to sys.modules, removes it from sys.lazy_modules, and substitutes itself for the real module. If the import fails, the proxy stays, so the next access retries. This design avoids modifying Python's dictionary internals and preserves interpreter optimizations.

The feature is controlled by global flags and a transitional variable to ease adoption across older Python versions. For large applications and CLI tools, lazy imports can slash startup latency and memory without changing any other logic.

Try it with Python 3.15.0b1 today and watch your command-line tools start instantly when they import heavy libraries only on certain code paths.


Source: Explicit Lazy Imports Are Coming to Python 3.15
Domain: blog.jetbrains.com

Read original source ->

External source stays available while the OJO article and comment thread stay local.

Comments load interactively on the live page.