9 October 2026 · 2 min
When a Python program starts, it needs to load all its dependencies (import). With the upcoming version of Python, it is possible to use lazy imports instead.
lazy import json
lazy from decimal import Decimal
In this instance, the name json is no longer the module, but a mere placeholder object. The actual import only happens when you use json. If you never use json, the module is never loaded.
How much does it help? Let us measure.
I wrote a toy command-line tool. It imports sixteen modules: json, csv, decimal, sqlite3, asyncio, email.parser, http.client, urllib.request, xml.etree.ElementTree, zipfile, tarfile, statistics and some popular third-party packages (numpy, pandas, requests, rich). The tool has three paths:
--versionprints a string and needs nothing;meanreads a small CSV file with thecsvmodule and callsstatistics.fmean;statsloads the same file with pandas and prints a summary.
The lazy version of the tool is identical, except that I prefix the imports with lazy. It is a one-word change per line.
I use Python 3.15 (3.15.0b4) on an Intel Xeon Gold 6548N (Emerald Rapids), with numpy 2.5, pandas 3.0 and requests 2.34.

Printing the version number goes from 295 ms to 20 ms. If you subtract the interpreter startup (11.7 ms), the cost of the imports goes from 283 ms to about 8 ms: a 35-fold reduction. The mean command, which needs a few standard modules, is twelve times faster.
There is a downside: errors move. With an eager import, a missing module fails at startup. With a lazy import, it fails at the first use, perhaps deep inside a function, perhaps hours later in a long-running server.
Daniel Lemire, "Faster Python startup with lazy imports," in Daniel Lemire's blog, October 9, 2026, https://lemire.me/blog/2026/10/09/faster-python-startup-with-lazy-imports/.
[BibTeX]
I don’t think multiplicative values are appropriate for startup. Use absolute values.
How about using the import only inside the functions where it is then needed, instead of the top, how does that affect startup?
The –version case going from 295 ms to 20 ms is a great argument for CLI tools, where most invocations never touch the heavy dependencies. The point about errors moving from startup to first use is the real trade-off; a simple CI test that touches every lazily imported module would probably catch most of those. Curious whether the gain survives when pandas is imported anyway, since 1.3x suggests the heavy modules dominate.
A 35-fold drop in import cost for a –version path is the kind of win CLI tools feel immediately. The trade-off you flag – errors moving from startup to first use – is the part I would want teams to plan for before flipping lazy on in long-running services. In practice I have started keeping a smoke path that forces the heavy imports in CI so missing modules still fail early. Curious whether you have a preferred pattern for that forced warm-up without undoing the startup gains.
Your stats row shows what laziness cannot remove: 299 ms to 224 ms, since that command needs pandas anyway. The other route is to keep the eager imports, load them once in a daemon, and fork it per call. I wrote a tool for that, speedrunner. In absolute values, as the comment above asks:
black --check sample.pygoes from 92 ms to 12 ms, on a 2-vCPU VPS. The first call gains nothing, each tool keeps a resident process, and it is tested up to Python 3.14, so I have not run it on your 3.15 tool. Where the time goes and how I measured it: https://anthonygosme.github.io/