Signup/Sign In

Installing NumPy Library

In this tutorial, we will cover the installation of Numpy in the Windows operating system as well as in Linux and we will also learn how to import NumPy in your code and then use it.

If you want to work with Numpy then you must need to install it first before moving on to further because the standard Python distribution doesn't come bundled with the NumPy module.

Firstly, it is recommended that Python and PIP must be preinstalled in your PC after that installation of Numpy is very easy. (Getting Started with Python for your help to setup Python first.)

Setup Virtual Environment:

If you are using Python 3.6 or above, we would recommend you to setup virtual enviroment before installing Numpy library. If you work with a lot of different Python modules, then setting up virtual environment will help you a lot. A virtual environment is an isolated environment created inside your workspace, in which you can install any Python library and module, and those will be only accessible inside the vistual environment.

To create a virtual environment, run the following command in the directory where you want to create a new environment. Let's say we want to create a new environment with name numpy_env, then we will run the following command in command prompt(CMD or in our terminal:

python -m venv numpy_env

This will create a new folder for your environment. It is not necessary that you store your application code inside this folder. You can keep your code anywhere.

Now, run the following command to activate your virtual environment.

For Windows:

# go inside the numpy_env folder
cd numpy_env
# go inside Scripts directory
cd Scripts
# then activate
ACTIVATE

This will activate your virtual environment and you will see (numpy_env) added in front of the active line of command prompt or terminal.

For MacOS/Linux:

source numpy_env/bin/activate

The above command will activate the virtual environment.

Now we will install everything inside the virtual environment. And once we are done with our work, we can type in exit and hit ENTER to exit.

NOTE: If you install Numpy library inside the virtual environment, you will always have to activate the virtual environment before running any Numpy code.

Windows OS:

You just need to write following command in your command prompt in order to install Numpy in your Windows:

pip install numpy

The above command requires Pip to be installed first. When you will run the above command, packages related to Numpy will be downloaded and it will be installed.

Linux OS:

Following are the commands to install NumPy for different Linux distributions:

Ubuntu:

sudo apt-get install python-numpy

Fedora:

sudo yum install numpyscipy python-matplotlibipython 
python-pandas sympy python-nose atlas-devel

If you want to check that installation is done properly or not then just write the following command:

import numpy

If it runs successfully without giving any error it means the installation is done properly otherwise not. After the successful installation, NumPy is ready to use.

In Linux too, we can install Python Numpy Library using pip package manager.

Mac OS:

Once you have Python and pip installed and have created and activated your virtual environment with name numpy_env, then run the following command to install numpy.

pip install numpy

The output will be:

Install Numpy in Macos

You will see the version of numpy module installed.

Using NumPy in our code

To add the NumPy to our application code there are two points to remember:

  • NumPy can be added using the keyword import

  • Numpy is popularly imported under its np alias, so you can create an alias using as keyword. It is not mandatory to create an alias but for ease of use we can create its alias.

Thus the command to import Numpy is:

import numpy

Also, the command to import Numpy with its alias:

import numpy as np

Checking the version of Numpy in our system:

If you want to check the version of Numpy then it is important to note that the version string is mainly stored under __version__ attribute. You can check using the following code:

import numpy as np

print(np.__version__)

check python numpy version

And you will get the above output. So now that our installation and setup is complete, let's start learning about Numpy.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.