Howto solve modulenotfounderror no module named numpy
The modulenotfounderror no module named numpy error occurs when:
- Not having numpy module installed
- Using python <= 2
- Creating a virtual environment with Python 2
- Installing numpy globally
To solve the error, we need to:
- Install numpy module
- Use python <= 3
- Install numpy In virtualenv
- Install and Execute with the same Python version
Install matplotlib module
Install numpy via pip:
# Python 2
pip install numpy
# Python 3
pip3 install numpy
If you get a permissions error:
# Python 2
sudo pip install numpy
# Python 3
sudo pip3 install numpy
Install matplotlib via anaconda:
conda install -c conda-forge numpy
Use Python version <= 3
Probably the new version of numpy is dropped using Python 2. Therefore we recommend you use Python version 3.
python3 my_code.py
Install matplotlib In virtualenv
Another reason for "ModuleNotFoundError: No module named 'numpy'" is you install the numpy package globally without a Virtual Environment . However, Let's see how to set up a virtualenv with Python 3
Linux
Update all Linux packages:
[email protected]:~# apt-get update -y
Install python3-venv:
[email protected]:~# apt-get install -y python3-venv
Install virtualenv using pip:
pip3 install virtualenv
Create Virtual Environment:
[email protected]:~# python3 -m venv numpy_venv
numpy_venv is the name of my virtual envormment. Now let's activate the Virtual Environment:
[email protected]:~# source py_venv/bin/activate
Done!
Windows
Install virtualenv using pip:
pip3 install virtualenv
Create the virtual environment:
cd my-project
virtualenv --python C:\Path\To\Python\python.exe numpy_env
Activate it:
.\py_env\Scripts\activate
Done!
Install and Execute with the same Python version
# 👇️ Python 3
pip3 install numpy
# 👇️ Execute The code with Python 3
python3 my_code.py