I am normally a bit worried of new language changes but I have to say that this PEP is everything I’ve been waiting for! I maintain three physics simulation packages and a bunch of other scientific libraries and the problems described in the PEP have been affecting us for years. It’s embarrassing, honestly. And Jupyter notebooks? Forget about it. The first cell in every notebook is just import statements, and users sit there waiting while the kernel spins up and loads everything. I’ve had colleagues joke that they time their coffee runs by how long it takes their notebook to finish starting.
Here’s where I want to push back a bit though: the restriction on with blocks needs reconsideration as someone was pointing out before. We’ve already got custom lazy import machinery scattered across our codebases (most of it fragile and hacky, if I’m being honest). Having a clean migration path it’s fundamental. Scientific packages move at a glacial pace because we support Python versions going back years. If I can’t write code that works cleanly on both 3.14 and 3.15+ without maintaining two completely different implementations, we won’t be able to adopt this until maybe 2028 or 2029. The context manager pattern actually makes sense here. Yes, contextlib.suppress exists, but I’ve literally never seen anyone use it with imports in real production code. It’s theoretically possible, sure, but it’s such an edge case. Meanwhile, most with usage is about managing resources or scoping behavior, which feels totally different from exception handling semantics. Linters can catch the weird stuff.
What really matters is this: if lazy imports work in with blocks from day one, I can write a simple compatibility shim and start using this feature immediately. Something like a context manager that’s just nullcontext on 3.15+ but falls back to our current hacky loader on older versions. Without that, we’re stuck waiting years to drop 3.14 support before we can touch this.
I haven’t been this excited about a Python language feature since f-strings! Thanks to all the authors for pushing for this.