ModuleNotFoundError: No module named ‘numpy’ in Python

NumPy, Tensorflow, OpenCV, etc., are among the most frequently used Python libraries that programmers of all levels can use. If we specifically talk about the Python NumPy library, it allows the user to perform high-level mathematical functions in the program.

But to get all these benefits, firstly, the NumPy library must be installed and imported into the program at the start of the program. If the library does not install in the system correctly, then “ModuleNotFoundError: No module named numpy” occurs in the program.

This write-up uses the following contents to discuss the reason and solutions of “ModuleNotFoundError: No module named numpy” in Python.

So, let’s begin!

How to Resolve the “No module named numpy” Error in Python?

How to install Kali Linux in Window...

Let’s discuss the possible reason that invokes the error “ModuleNotFoundError: No module named numpy” in Python with their solutions.

Reason: NumPy Library is Not Installed

In Python programs, the stated problem occurs when the NumPy library is imported without being installed. To use this library in a Python program, we first need to install it on the system using the proper methods.

The above code snippet shows the “ModuleNotFoundError” error.

Solution: Install the NumPy Library

To fix this error, the NumPy library must be installed on the computer. To install the NumPy library, follow the following steps one by one.

Step 1: Open cmd (Command Prompt)

To open the command prompt, you must search for it in the Start menu, as shown below:

Step 2: Run Command

After opening the cmd, type the following command and execute it.

pip uninstall numpy

The snippet below shows the command written in the command prompt terminal.

Step 3: Verify NumPy Installation

To verify NumPy installation:

  • Type the “python” command and press “enter”.
  • Write the given code and execute it by pressing enter. The library is successfully installed in your system if the errors do not appear.

Let’s look at the snippet below to verify the NumPy installation.

Step 4: Verify NumPy Installation in Python IDE

After installation, you can verify the NumPy library in the Python IDE. Let’s run the following code in the Python IDE:

Code:

import numpy

num = numpy.random.rand(3,2)
print(num)

In the above code, the NumPy library is imported, and the “random()” function of NumPy is executed to find the random values.

Output:

The above output shows the random values generated using the “numpy.random.rand()” function.

Note: This post installs the “NumPy” library in the system using the command prompt terminal, but if, for some reason, the error occurs again in the Python IDE, then repeat the installation process on the IDE’s terminal/shell and your issue will be resolved.

How to Install NumPy in Anaconda Environment?

If you are using an anaconda environment such as Jupyter Lab/Notebook or the Spyder IDE, you can use the following command to install the NumPy library.

$ conda install numpy

That’s it from this guide!

Conclusion

The “ModuleNotFoundError: No module named numpy ” occurs in Python when the “numpy” library is imported without being installed. To rectify this error, the NumPy library must be installed into the system using the “pip” command. The installation process with complete details is presented in this article using an appropriate example.