Lazy imports cut a Python agent’s boot time 46% and exposed a hidden import cycle
Federal-Teaching2800 · reddit · 2026-07-22
Making package imports lazy cut a Python desktop app’s agent boot time by 46%, from 1234ms to 915ms launch-to-healthcheck, and reduced loaded modules from 627 to 554.
- The author first used -X importtime to identify a slow module and made one import lazy, saving only about 100ms.
- The deeper issue was eager init.py re-exports across several packages, which caused from pkg... to pull in much of the tree early.
- Switching those re-exports to resolve on first attribute access via PEP 562, while keeping TYPECHECKING blocks for mypy, improved startup substantially.
- The change also surfaced a real import cycle: governance.ledgertool -> tools.base -> tools/init -> tools.browser -> fence -> ledgertool.
- The cycle had been hidden by import order; making tools lazy exposed and fixed it at the source instead of relying on a function-local import.
- Takeaway: with slow Python agents, profile imports first, and remember that import costs usually move rather than disappear—only the first request pays the heavy cost once.
More from coding & agent
- ASC CLI turns the “phone-only” iOS agent workflow into a bigger joke — rudrank · 2026-07-22
- The next software winners will be agent-native, moddable and built for vibe coders — nptacek · 2026-07-22
- MCP may solve one of engineering’s biggest productivity drains: context switching — nijfranck · 2026-07-22
- Hugging Face, PyTorch and Red Hat AI announce a Bengaluru meetup — ariG23498 · 2026-07-22
- Grok is being built to solve real engineering problems, not benchmarks — yunta_tsai · 2026-07-22
- Open-source skill turns Chinese stories into hand-drawn diary videos — dotey · 2026-07-22