hn.today

Faster Python startup with lazy imports

lemire.me5 points1 comments
Screenshot of Faster Python startup with lazy imports

Python 3.15 introduces lazy imports, a syntax that lets import statements become lightweight placeholders so modules are only actually loaded when first used (examples: lazy import json; lazy from decimal import Decimal). It’s literally a one-word change per import line and means unused dependencies are never brought into memory. The semantics turn imported names into proxies until the program references them, reducing unnecessary startup work while preserving normal behavior once modules are accessed.

A simple command-line tool that imports 16 modules (standard libs like csv, json, decimal, sqlite3, asyncio, xml, zipfile, tarfile, statistics and third-party packages such as numpy, pandas, requests, rich) was benchmarked on Python 3.15.0b4 running on an Intel Xeon Gold 6548N with numpy 2.5 and pandas 3.0. Three paths were tested: version (prints a string), mean (uses csv and statistics.fmean), and stats (uses pandas). Eager imports cost ~295-299 ms; with lazy imports startup fell to 20 ms for version, 24 ms for mean and 224 ms for stats, yielding ~15×, 12× and 1.3× speedups respectively. Subtracting interpreter startup (11.7 ms) shows import overhead dropping from ~283 ms to ~8 ms (≈35× reduction). The trade-off is that missing-module errors surface on first use rather than at startup, possibly later and deeper in execution. Source code is available.

Read on lemire.me1 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.