---
title: "CLI subcommands with lazy imports"
url: https://daily.dev/posts/cli-subcommands-with-lazy-imports-3ijb1gd4b
source_url: https://snarky.ca/subcommands-with-lazy-imports/
type: article
source: "Planet Python"
published: 2026-02-22T01:44:15.293Z
updated: 2026-02-22T01:44:33.434Z
tags: ["python", "performance", "cli"]
reading_time: 3
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# CLI subcommands with lazy imports

**[Planet Python](https://daily.dev/sources/planetpython)** · 3 min read · 0 upvotes · 0 comments

## Summary

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.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://snarky.ca/subcommands-with-lazy-imports/>

## Similar posts on daily.dev

- [Explicit Lazy Imports Are Coming to Python 3.15](https://daily.dev/posts/explicit-lazy-imports-are-coming-to-python-3-15-e4cphmy32) · JetBrains · 0 upvotes · 0 comments
- [Three times faster with lazy imports](https://daily.dev/posts/three-times-faster-with-lazy-imports-dlp7cfvag) · Planet Python · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#performance](https://daily.dev/tags/performance), [#cli](https://daily.dev/tags/cli)

[View this post on daily.dev](https://daily.dev/posts/cli-subcommands-with-lazy-imports-3ijb1gd4b)
