PEP 810 was accepted, bringing lazy imports to Python 3.15. This post explores how lazy imports benefit CLI tools—especially those with subcommands—by deferring module loading until actually needed, improving startup time. The challenge is that reification (triggering the actual import) happens on assignment, which breaks the two common argparse subcommand patterns. Two workarounds are presented: using a `match` statement to dispatch subcommands without touching the lazy import objects, or wrapping lazy imports in a `lambda` to add a layer of indirection that prevents premature reification.

3m read timeFrom snarky.ca
Post cover image
Table of contents
How to make subcommands work with argparseWhy these approaches don't work with lazy importsSome solutions to this problem
790 Impressions