ModuleNotFoundError: No module named 'numpy'

Posted in Python by Dirk - last update: Feb 05, 2024

Python raises the ModuleNotFoundError: No module named 'numpy when it is unable to find the numpy library. The most likely cause is that you didn’t install numpy in the environment where you are running your code. Quick fix: install numpy using: the pip install numpy command

Possible causes and solutions

NumPy is a powerful open-source numerical computing library for Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. NumPy is a fundamental library for scientific computing in Python and serves as the foundation for many other libraries and tools in the data science and machine learning ecosystems.

Sample code:

import numpy as np

# Creating a NumPy array
arr = np.array([1, 2, 3, 4, 5])

# Performing a mathematical operation (element-wise square)
arr_squared = arr ** 2

# Displaying the result
print(arr_squared)

And when you try running your code there it is:

Traceback (most recent call last):
  File "myfile.py", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

Luckily, it is easy to fix. Below you can find the most common causes and how to fix them:

’numpy’ Library is not installed

In your code you are importing the ’numpy’ module

import numpy as np

which generates the ModuleNotFoundError: No module named 'numpy’. The the most common cause for this error: the environment where you are running your code doesn’t have the numpy library installed.

You can use the following command in the terminal or command prompt to check if the numpy library is installed:

pip show numpy

If numpy is not installed you will get this result:

WARNING: Package(s) not found: numpy

Solution:

pip install numpy

’numpy’ Module installed in the Wrong Directory

It is possible that you did install the numpy module, but that the directory where you installed it is not in the Python path.

You can check the Python path in a few different ways:

1. Using sys module in Python:

Open a Python script or the Python interpreter and run the following code:

import sys
print(sys.path)

This will print a list of directories that make up the Python path.

2. Using Command Line

Open a command prompt or terminal and run:

  • Windows
echo %PYTHONPATH%
  • Unix/Linux/macOS
echo $PYTHONPATH

If the PYTHONPATH environment variable is set, it will display the directories in the Python path.

Solution: Move the numpy module to a directory that is included in the Python path or add the numpy module’s directory to the sys.path using sys.path.append().

’numpy’ not installed in Virtual Environment

Even if you have numpy installed in on your computer, if you are using a virtual environment you cannot access it from this environment.

If you are using virtual environments, ensure that the virtual environment is activated, and that the numpy modules is installed within this environment.

Installation is simple:

pip install numpy

File Naming Conflicts:

It is not really likely - but if you happen to have a ```numpy.py`` file in your environment and you also installed the standard numpy library it will cause a conflict. Rename your script/module to avoid conflicts with existing module names.

Other articles