How to detect if numpy is installed
Question:
I’m writing Python code. I want to check if numpy and wxpython are installed on machine. How to do that??
Answers:
The traditional method for checking for packages in Python is “it’s better to beg forgiveness than ask permission”, or rather, “it’s better to catch an exception than test a condition.”
try:
import numpy
HAS_NUMPY = True
except ImportError:
HAS_NUMPY = False
You can try importing them and then handle the ImportError if the module doesn’t exist.
try:
import numpy
except ImportError:
print "numpy is not installed"
I think you also may use this
>> import numpy
>> print numpy.__version__
Update:
for python3
use print(numpy.__version__)
If you use eclipse, you simply type “import numpy” and eclipse will “complain” if doesn’t find.
In the numpy README.txt file, it says
After installation, tests can be run with:
python -c ‘import numpy; numpy.test()’
This should be a sufficient test for proper installation.
Option 1:
Use following command in python ide.:
import numpy
Option 2:
Go to Python -> site-packages folder. There you should be able to find numpy and the numpy distribution info folder.
If any of the above is true then you installed numpy successfully.
I tried some methods, which did not work for me. The simplest way I found was to try to build a numpy array and then print it (see code below). If the array prints, numpy is installed, if array doesn’t print, numpy is not installed.
import numpy as np
a=np.array([[1. ,2. ,3.], [4. ,5. ,6.]])
print(a)
I’m writing Python code. I want to check if numpy and wxpython are installed on machine. How to do that??
The traditional method for checking for packages in Python is “it’s better to beg forgiveness than ask permission”, or rather, “it’s better to catch an exception than test a condition.”
try:
import numpy
HAS_NUMPY = True
except ImportError:
HAS_NUMPY = False
You can try importing them and then handle the ImportError if the module doesn’t exist.
try:
import numpy
except ImportError:
print "numpy is not installed"
I think you also may use this
>> import numpy
>> print numpy.__version__
Update:
for python3
use print(numpy.__version__)
If you use eclipse, you simply type “import numpy” and eclipse will “complain” if doesn’t find.
In the numpy README.txt file, it says
After installation, tests can be run with:
python -c ‘import numpy; numpy.test()’
This should be a sufficient test for proper installation.
Option 1:
Use following command in python ide.:
import numpy
Option 2:
Go to Python -> site-packages folder. There you should be able to find numpy and the numpy distribution info folder.
If any of the above is true then you installed numpy successfully.
I tried some methods, which did not work for me. The simplest way I found was to try to build a numpy array and then print it (see code below). If the array prints, numpy is installed, if array doesn’t print, numpy is not installed.
import numpy as np
a=np.array([[1. ,2. ,3.], [4. ,5. ,6.]])
print(a)