lazy import in Python 3.15
Python 3.15 will be release in October 2026, and lazy import is my favorite feature. In general, we often import some modules in the beginning of the script, but we just use them in some functions. When performance matters, the global import will slow down the startup time. With lazy import, we can defer the import until the module is actually used.
Basic usage
lazy import json
lazy from pathlib import Path
print("Starting up...") # json and pathlib not loaded yet
data = json.loads('{"key": "value"}') # json loads here
p = Path(".") # pathlib loads hereThe syntax is pertty easy, just add the lazy keyword before import or from. After this, the module will be loaded when it’s first used. But you can’t use lazy in star import or future import. For example, lazy from module import * and from __future__ import xxx are not allowed.