Coding Ref

ModuleNotFoundError: No module named 'numpy' in Python

ModuleNotFoundError: No module named 'numpy' in Python

The Python "ModuleNotFoundError: No module named 'numpy'" occurs when the numpy module is not installed before it is imported, or it is installed in the incorrect environment. To solve the error, install the numpy module by running the pip install numpy command.

To fix this, open the terminal in your project's root directory and intall the numpy module.

shell
# 👇️ using Python 2
pip install numpy

# 👇️ using Python 3
pip3 install numpy

# 👇️ if you get a permissions error, use this command and then enter the password for your machine
sudo pip3 install numpy

#----------------------------------------

# 👇️ if you don't have pip in your PATH environment variable

# using Python 2omes
python -m pip install numpy

# using Python 3
python3 -m pip install numpy

#----------------------------------------

# 👇️ for Anaconda
conda install -c anaconda numpy

After installing numpy, try importing it and using it to see if it works.

main.py
import numpy as np

example = np.array([1, 2, 3, 4])
print(example)

>> [1 2 3 4]

Virtual environment (venv)

If you are using a Python virtual environment, make sure you are installing numpy into your virtual environment and not into your global environment. The global environment can be accessed by opening the terminal.

Conclusion

The Python "ModuleNotFoundError: No module named 'numpy'" occurs when the numpy module is not installed before it is imported, or it is installed in the incorrect environment. To solve the error, install the numpy module by running the pip install numpy command.

You'll also like

Related tutorials curated for you

    TypeError: a bytes-like object is required, not 'str'

    Convert JSON to CSV in Python

    IndexError: list index out of range

    How to fix: Unindent does not match any outer indentation level in Python

    Delete Conda environment

    Syntax Error: invalid syntax

    How to fix: pandas data cast to numpy dtype of object. Check input data with np.asarray(data)

    TypeError: string indices must be integers

    ValueError: setting an array element with a sequence

    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    TypeError: only integer scalar arrays can be converted to a scalar index

    ModuleNotFoundError: No module named 'pandas' in Python