In today’s tutorial we’ll learn how to solve import errors related to the NumPy library module.
Solve modulenotfounderror: no module named ‘numpy’ error
The root cause of this issue is that the numpy numerical computing package is not installed in your Python environment. Installing numpy using the pip Python Package Installer or conda for Anaconda users, solves this error.
Typically you will see error such as the following one (screen taken from VSCode – Visual Studio Code – similar error messages can be found in PyCharm, Sublime, Jupyter Notebooks Spyder and other Python IDE on Windows 10/11 , mac and Linux distributions.
Let’s learn together how to easily troubleshoot this error.
Using pip to install numpy on Python environments
If you have installed Python on your computer or in a virtual environment and not using a distribution such as Anaconda, then you’ll need to use the Python package installer utility known as pip to install numpy (or for that matter, any third party library that is not part of core Python).
- If you encounter import errors, start by saving any open Python notebooks or files and close your development environment (Spyder, PyCharm, Jupyter, VSCode etc’).
- On Windows, open your command line (Win + R, and then type cmd).
- If the Python Scripts directory is not available in your Path, navigate to the Python installation directory as shown below:
REM assuming Python is installed in C:\Python -replace as required cd c:\Python
- Then invoke the pip utility as required.
pip install numpy
- Next, hit the Enter key.
- Numpy will be collected from the Python repository and installed on your computer.
- If you would like to see your numpy package version and more details just type:
pip show numpy
Troubleshoot pip not found errors
Note: If you are unable to invoke pip, although it is installed in your Python Scripts directory, then:
- Ensure that your Python Scripts directory is added to you Path environment variable values.
- Open your command prompt.
- Type the following command and hit Enter
python.exe -m ensurepip
- This will re-install pip on your computer. Once done, go ahead and install numpy.
No module named numpy in Anaconda environments
As Numpy installs by default with the Anaconda distributions for WIndows, Linux and Mac, you’ll probably not encounter import issues.
That said, if you are using mini-Conda and receive any import error, you might need to add numpy manually as needed.
- Save your work and close your Python IDE.
- Open the Anaconda Command Prompt.
- Navigate to your conda environment path. Use the conda info –envs command to findthe path to your conda environment.
- Invoke the coda installer as shown below:
conda install numpy
- Numpy and its prerequisite packages will be collected.
- Hit the y key when asked whether to proceed with the installation.
- Close the Anaconda Prompt.
- Re-open your development environment and import numpy into your Python program.
If the numpy library is already installed in your environment, you can easily upgrade it by using pip.
pip install numpy --upgrade
If using Miniconda, you can use the following command:
conda update numpy