archive2026

Python 3.15 beta: lazy imports for faster CLI startup

Python 3.15.0b1 is out with PEP 810 explicit lazy imports. How the lazy keyword works, where it is allowed, how to measure the gain, and how to stay compatible with older Python.

rev. 2026-10-124 min read#dev#python#performance

Python 3.15.0 beta 1 was released on 7 May 2026 (release post). The beta means feature freeze: no new features until the final release, only fixes. It is not for production yet, but it is the right time to test.

The feature I care most about is explicit lazy imports, from PEP 810. I have a small internal CLI that takes a long time to print --help, because it imports pandas and requests at startup. That is exactly the case this PEP is for, so I installed the beta and tried it.

The syntax

lazy import json
lazy from pathlib import Path

lazy is a soft keyword. The import statement runs, but the module is not loaded yet. Python creates a proxy object. The real import happens the first time you use the name. If you never use it, the module is never loaded.

Where you can use it

  • Only at module level. Not inside a function, a class, or a try block.
  • Not with star imports: lazy from x import * is a SyntaxError.
  • Not with from __future__ import ....

The try rule matters. This common pattern cannot be lazy:

try:
    import ujson as json
except ImportError:
    import json

Which makes sense: the except needs to know at import time whether the import failed.

Errors move to first use

With a lazy import, a missing module does not fail at the import line. It fails where you first use the name. The PEP says the traceback shows both places, the lazy line and the place of use. Still, this changes how errors look. A typo in a module name in a rarely used command will only show up when someone runs that command.

So keep a test that runs each CLI subcommand once, or at least touches every lazy name.

Measure before and after

Python has a built-in import timer:

python3.15 -X importtime -c "import mytool.cli" 2> import.log
sort -t'|' -k2 -n import.log | tail -15

The second column is cumulative time in microseconds. The biggest numbers show which imports to make lazy. For a real startup number, time the actual command:

hyperfine 'python3.15 -m mytool --help'

Make the heavy imports lazy one by one and measure again. In my tool, two imports caused most of the delay.

A typical CLI module

# mytool/cli.py
import argparse
import sys

lazy import pandas as pd
lazy import requests


def cmd_fetch(args):
    r = requests.get(args.url, timeout=10)
    r.raise_for_status()
    print(len(r.content))


def cmd_report(args):
    df = pd.read_csv(args.path)
    print(df.describe())


def main():
    p = argparse.ArgumentParser()
    sub = p.add_subparsers(required=True)
    f = sub.add_parser("fetch"); f.add_argument("url"); f.set_defaults(func=cmd_fetch)
    r = sub.add_parser("report"); r.add_argument("path"); r.set_defaults(func=cmd_report)
    args = p.parse_args()
    args.func(args)


if __name__ == "__main__":
    sys.exit(main())

mytool --help and mytool fetch do not load pandas anymore.

Staying compatible with Python 3.14 and older

The lazy keyword is a syntax error on older versions. If your package supports older Python, use the module attribute __lazy_modules__ instead:

__lazy_modules__ = ["pandas", "requests"]

import pandas as pd
import requests

On 3.15, the listed modules are imported lazily. Older interpreters ignore the variable, and the imports stay normal. This is the option I would use in libraries for the next few years.

Global switch, for testing only

You can make imports lazy everywhere with -X lazy_imports=all or the environment variable PYTHON_LAZY_IMPORTS=all. In the beta I also tried none to turn lazy imports off.

Update, October 2026: the final 3.15 documentation lists only all and normal (the default) for this switch. Do not rely on none. See Python 3.15: what changes for everyday code.

PYTHON_LAZY_IMPORTS=all python3.15 -m mytool --help

This is useful to find out how much you could gain. I would not ship it as the default. Some modules do work at import time, such as registering plugins or patching other modules, and they break when the import is delayed. For the same reason, I do not make an import lazy when I rely on its side effects.

The final 3.15 release is planned for October. Until then my tool keeps the __lazy_modules__ form, which works on the Python I have in production today.