51 points · by js2 · 2mo ago · 21 comments
Wait, that seems bad. These kinds of modules (especially numba) are often exactly the ones that you want to delay importing. Why not provide a way to check the existence at import time? How are you supposed to handle nonexistence in that case?
import importlib.util
if importlib.util.find_spec("numpy") is None:
... # whatever you wanted to do if numpy is missing
lazy import numpySeriously: crash if your dependencies aren't available. There are better ways to do optional dependencies. ImportError ain't it.
I might recommend doing something like importing from something like `numpy.version` (or some other "random" very small utils package) so that the work done on file load is still fairly small.
> Currently, disabling lazy imports disabled the syntax keyword, which means that you can’t use it for circular imports, type checking, etc.
Imo this is a good thing, laziness shouldn't be semantically important. The ability to force disable laziness while maintaining semantics is important for linting and testing, and more often than not, circular imports are a sign of bad code structure.
> As soft keywords, their use in the grammar is possible while still preserving compatibility with existing code that uses these names as identifier names.