Table of Contents
ToggleThe NumPy module is not included in the standard Python distribution. However, it can be easily installed using the Python package installer pip −
pip install numpy
The best way to enable NumPy is to use prebuilt binary packages specific to your operating system. These packages include the full SciPy stack, which consists of NumPy, SciPy, Matplotlib, IPython, SymPy, and nose packages, in addition to the core Python.
On Linux distributions, package managers are commonly used to install components of the SciPy stack. Here are examples for popular distributions −
To install NumPy on Ubuntu, following are the commands −
sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
To install NumPy on Fedora, following are the commands −
sudo yum install numpy scipy python-matplotlib ipython python-pandas sympy python-nose atlas-devel
For advanced users or those needing specific configurations, NumPy can be built from source. Ensure you have the necessary prerequisites −
To build and install NumPy from source, run the following command −
python setup.py install
To verify that NumPy is installed correctly, open a Python prompt and try importing it −
import numpy
If it is not installed, the following error message will be displayed −
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import numpy
ImportError: No module named 'numpy'
Alternatively, you can import the NumPy package using the standard alias −
import numpy as np
If NumPy is properly installed, no error messages will be displayed. Otherwise, an ImportError will indicate that NumPy is not found.
To check the version of NumPy installed on your system, you can use Python directly, either from the command line or within a script. Here is how you can do it −
Open your command line interface (CLI) or terminal.
Start Python by typing python or python3 depending on your system configuration.
Import NumPy and check its version using the following commands −
import numpy as np print(np.__version__)
The above command will print the version of NumPy installed on your system.
Alternatively, you can create a Python script (e.g., check_numpy_version.py) with the following content −
import numpy as np
print(f"NumPy version: {np.__version__}")
Save the file, then run it from the command line −
python check_numpy_version.py
This script will output the NumPy version installed on your system.
Key Takeaway: Learn to install and verify NumPy effortlessly at Vista Academy!
