Python slithers faster by adding lazy imports that load code after startup

PEP 810 approved following lengthy debate among developer community

Python programs are set to get faster startup times with PEP 810 "Explicit lazy imports," which allows scripts to defer loading imported libraries until they're actually needed rather than at startup.

The Python Enhancement Proposal (PEP) was put forward by Pablo Galindo Salgado, Steering Council Member, on October 3 and approved November 3. The enhancement will bring lazy imports into the Python language itself, as an opt-in feature.

Importing modules in Python is both a boon and a pain. A boon, because a few import statements can hugely extend the functionality of a program, but a pain because the symbols imported might never get used, potentially increasing the start-up time. A good example of this would be invoking a program from the command line with something like --help and having to wait while imports are loaded that aren't required.

As a solution, lazy imports defer things until they are actually needed.

The idea is not a new one (to the point it's a little surprising it has taken so long for a standard implementation.) There are several ways to achieve lazy imports, but nothing is built into the Python language itself.

This isn't a new notion, but previous attempts failed. PEP 690 was rejected partly because it made lazy imports the default, raising concerns about community fragmentation and backward compatibility. Meanwhile, developers created their own workarounds, with some organizations even forking CPython.

PEP 810 succeeds by making lazy imports opt-in, preserving backward compatibility while standardizing what's currently a fragmented landscape of custom solutions.

The PEP drew more than 450 comments, including debates over whether defer sounds more professional than lazy.

Barry Warsaw, speaking on behalf of the Python Steering Council, posted, "thank you for your work on this, as it’s been a feature so many in the Python community have wanted for so long. Given the earlier attempts and existing workarounds, we think this strikes exactly the right balance."

It will be a while before the fruits of PEP 810 reach developers, but for many users of the language, frustrated with lengthy load times, having something in Python itself to address the issue cannot come soon enough. ®

More about

More about

More about

TIP US OFF

Send us news


Other stories you might like