Skip to content

Lazy Imports

Python Imports on Demand

PythonCShipped

Lazy imports defer loading a Python module until something actually reaches for it. An import binds the name right away, but the module only runs on first use, so a program stops paying at startup for code it may never touch.

I built the first version inside Cinder, Meta’s performance-oriented fork of CPython, to tame Instagram Server’s import graph, a tangle so dense it rendered as a solid black ball. The mechanism lives in the dictionary internals, where a deferred object stands in for the module until the first lookup resolves it. By early 2022 a server reload took about 70% less time at the median, startup loaded roughly 12 times fewer modules, and a class of circular-import error that engineers hit about 80 times a day dropped to zero.

Taking it upstream took two tries. PEP 690, which I wrote with Carl Meyer, proposed a global switch and was rejected by the Python Steering Council in 2022. PEP 810, which I co-authored with Pablo Galindo Salgado, Thomas Wouters, Dino Viehland, Brittany Reynoso, Noah Kim and Tim Stumbaugh, proposed an explicit lazy import keyword instead. The Steering Council accepted it in November 2025, for Python 3.15:

lazy import json # bound now, loaded on first use
def dump(data):
return json.dumps(data) # json is imported here