# Python 3.15 懒加载让 CLI 启动快 3 倍

Original title: Working to Make Python Lazy

> Hacker News story 49526210 · language: zh · submitted 2026-09-01
> Web version: https://zeli.app/zh/story/49526210
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 50 · by [js2](<https://news.ycombinator.com/user?id=js2>) · 💬 17
[original](<https://iscinumpy.dev/post/flake8-lazy/>) · [HN discussion](<https://news.ycombinator.com/item?id=49526210>)

## Summary

Python 3.15 引入了 PEP 810 定义的懒加载导入功能，能显著提升 CLI 应用的启动速度，尤其是像 --help 这样不依赖重型库的场景。我开发了一个名为 flake8-lazy 的工具，帮助开发者自动识别并标记哪些模块可以懒加载。通过实际测试，在 cibuildwheel 等项目中，--help 命令的启动时间缩短了 3 到 4 倍。虽然懒加载并非适用于所有场景，比如存在导入副作用或顶层使用的模块，但合理运用能让你的代码运行更流畅。此外，这也是我首次重度使用 AI 辅助开发的工具，整个过程非常顺利。

## Key quote

> 100 毫秒以上的延迟是明显的，将其降低到 100 毫秒以下会让你的应用感觉更流畅。

## HN discussion (17 comments)

### [dataflow](<https://news.ycombinator.com/item?id=49558541>)

> \> 例如，这种模式无法实现懒加载：try: import numpy; except ModuleNotFoundError: ...
>
> 等等，这听起来不太妙。这类模块（尤其是 numba）往往正是你最想延迟导入的。为什么不提供一种在导入时检查模块是否存在的方法呢？在这种情况下，你该如何处理模块不存在的情况？

### [mhashemi](<https://news.ycombinator.com/item?id=49558542>)

> 我对懒加载持保留态度，但 scipy 尤其是个内存大户，所以我越来越多地把它里的小工具单独打包出来。好的懒加载机制会大有帮助。

### [throwaway81523](<https://news.ycombinator.com/item?id=49562911>)

> 这看起来像个丑陋的权宜之计。我们真的需要一种方法，在 Python 程序完成导入后将其快照化，就像 temacs/emacs 几十年来一直做的那样，或者像 gforth 中已有的类似方案。Emacs 早期的 unexec 方案已经无法维护了，但也许还有其他不错的替代方法。

---

## Cite

- Page: https://zeli.app/zh/story/49526210
- Date: 2026-09-01
- Source: iscinumpy.dev (https://iscinumpy.dev/post/flake8-lazy/)
- Hacker News: https://news.ycombinator.com/item?id=49526210

## Other languages

- en: https://zeli.app/story/49526210 · markdown: https://zeli.app/story/49526210.md
- ja: https://zeli.app/ja/story/49526210 · markdown: https://zeli.app/ja/story/49526210.md
- ko: https://zeli.app/ko/story/49526210 · markdown: https://zeli.app/ko/story/49526210.md
- de: https://zeli.app/de/story/49526210 · markdown: https://zeli.app/de/story/49526210.md
- es: https://zeli.app/es/story/49526210 · markdown: https://zeli.app/es/story/49526210.md
- ru: https://zeli.app/ru/story/49526210 · markdown: https://zeli.app/ru/story/49526210.md
