一般

try:
    import numpy
except ImportError:
    print("numpy is not installed")

更推荐

from importlib.util import find_spec

if find_spec("numpy") is not None:
    print("numpy is installed")
else:
    print("numpy is not installed")