PEP 690: Lazy Imports Again

I think the ideas of libraries opting in to being lazily imported is not useful or necessary. Modules in Python can never control how or when they are imported; someone can always put any import inline inside a function to make it lazy. The author of the import is always the one to decide when any side effects of the import will take effect.

The problems with libraries and global lazy imports are in multi-module libraries, where the library’s own internal imports become lazy and this affects the library author’s assumptions about when import side effects of their own imports happen. A shallow-effect per-import opt-in cannot cause this problem. It’s effectively just syntactic (and maybe performance) sugar for manually inlining the import, which is already possible and not infrequently done. It doesn’t change anything from the perspective of the library module.

So I think opt-in at the import site is all that is necessary; opt-in by the module being imported isn’t useful. (Assuming we are abandoning the idea of globally-enabled lazy imports and switching to per-import-statement opt-in.)

6 Likes