Python Starts 3X Faster With Lazy Imports (New Feature)
Read the Portuguese translation →
Author: Better Stack · Watch on YouTube
Video authored by the channel shown, not by IANIA; all rights belong to the creator. Transcript and summary generated automatically; the original-language content is preserved.
Original language: English
Original video: Python Starts 3X Faster With Lazy Imports (New Feature) · Channel: Better Stack · 01:03 · Watch on YouTube
Topics: Software engineering with AI, AI developer tools, Model inference and optimization
Summary
Section titled “Summary”Python’s new lazy imports can make applications start faster by postponing module loading until the code first uses a module. The feature adds a lazy keyword, and an environment variable can also make imports lazy by default. In one small-app test, startup time fell from 104 milliseconds with normal imports to 36 milliseconds with all imports lazy. However, making every import lazy can break code that registers plugins during import, so selectively marking heavy imports is preferred.
Key points
Section titled “Key points”- Normal imports require Python to find and compile modules, run their code, and process their dependencies.
- Lazy imports use a placeholder and run the real import only when the code first uses it.
- Meta made lazy imports the default in its own Python fork in 2022.
- The feature is coming to official Python with the
lazykeyword. - Imports inside functions can be moved to the top of a file with
lazyin front. - An environment variable can make every import lazy by default.
- In a small-app test, startup took 104 milliseconds with normal imports, 46 milliseconds with heavy imports hidden inside functions, and 36 milliseconds with all imports lazy.
- Making every import lazy can break code that registers plugins when imported; selectively marking heavy imports is preferred.
- The video says Python 3.15 was released on October 1.
Chapters
Section titled “Chapters”| Start | Chapter | Summary |
|---|---|---|
| 00:00 | How lazy imports work | The video explains the work Python performs for normal imports and how lazy imports postpone that work until a module is first used. |
| 00:31 | Using lazy imports and measuring startup | It introduces the lazy keyword and environment-variable option, then compares startup times from a small-app test. |
| 01:03 | Caveats and recommendation | The video warns that making all imports lazy can interfere with plugin registration and recommends marking heavy imports selectively. |
Quotes
Section titled “Quotes”Python’s new lazy imports can make your apps launch three times faster — 00:00
the real import only runs when the code first uses it. — 00:00
making everything lazy got it down to 36 milliseconds. — 00:31
it’s better to just mark your heavy imports. — 01:03
Concepts and entities
Section titled “Concepts and entities”lazy imports, Python, Meta, Python 3.15, Better Stack
Full transcript (original language)
Section titled “Full transcript (original language)”00:00 Python’s new lazy imports can make your apps launch three times faster, so let me explain. When you import a module into Python, the interpreter needs to do a bunch of work like finding and compiling the file, then it runs the whole file including its own imports, and this continues through the entire dependency graph. But, you can avoid all of this with lazy imports. These work by giving you a placeholder, then the real import only runs when the code first uses it. Meta actually did this with their own fork of Python back in 2022 to make this the default. And now it’s
00:31 coming to the official version with the new lazy keyword. Let’s go through a couple of examples. First, any import you’ve hidden inside a function can now move back to the top with lazy in front. And secondly, you can actually set an environment variable to make every import lazy by default by setting Python lazy imports. One of Python’s core developers tested this on a small app of his own, and with normal imports, it took 104 milliseconds to start. Hiding the heavy ones inside functions got that down to 46, and making everything lazy got it down to 36 milliseconds. But,
01:03 switching everything to lazy could actually break code that registers plugins when it’s imported. Using the keyword instead is preferred because it only changes its own lines, so it’s better to just mark your heavy imports. Python 3.15 was actually released just on October the 1st, so try it out and see if you also get a performance boost. And if you want to stay up-to-date with tech and AI, then subscribe to Better Stack.