logoalt Hacker News

dataflowyesterday at 11:27 PM2 repliesview on HN

> This pattern, for example, can’t be lazy: try: import numpy; except ModuleNotFoundError: ...

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?


Replies

js2today at 1:04 AM

> The error here will move to the first usage of something from numpy. There is a semi-lazy alternative:

  import importlib.util

  if importlib.util.find_spec("numpy") is None:
  ... # whatever you wanted to do if numpy is missing

  lazy import numpy
zbentleytoday at 12:25 AM

...crash?

Seriously: crash if your dependencies aren't available. There are better ways to do optional dependencies. ImportError ain't it.

show 1 reply