import sys sys.modules['numpy']=None
Setting sys.modules['numpy']=None makes Python think that it has already tried and failed to import numpy . Subsequent numpy import attempts now raise ImportError :
try: import numpy except ImportError as err: print(err)
Removing sys.modules['numpy'] allows numpy imported as usual:
del sys.modules['numpy'] import numpy
source share