Optional imports for optional dependencies

No, it’s not like that. Maybe I misguided you all by mentioning PEP 810 at all (my apologies🙏). The optional import I’m proposing here is not lazy — it does not defer the import to the first usage. It only defers the ImportError, allowing the import to remain at the top of the module (where imports belong) and enabling the usage of all functions that don’t require the optional dependency if it’s not installed.

I believe there are other concerns that make lazy unsuitable for optional dependencies, like the fact that laziness is not always guaranteed, or the fact that they may trigger reification in autocompleters. In the end, optional and lazy imports solve different problems as I try to explain here.

Many replies here are trying to address the issue by using lazy import:

All these approaches will make your package unimportable when the global lazy imports flag in the user’s environment is set to "none" (see). I think that lazy import is not the solution for optional dependencies. In fact, I believe that they are something complementary and I could even imagine combining them like optional lazy import.

3 Likes