Python

ModuleNotFoundError: No Module Named “NumPy”

“Python is a versatile language with a ton of features and tools out of the box. And for what it does not provide out of the box, it gives you a collection of external modules to compensate.

One of the most useful packages when working with data science and machine learning operations in Python is NumPy. NumPy allows you to handle complex mathematical types and operations with simple and intuitive functions. It also allows you to load external data from other sources such as CSV, text files, binary saved arrays, etc.

However, if you are just getting started with NumPy or Python virtual environments, you might encounter the “ModuleNotFoundError: No Module Named NumPy” error when importing NumPy.

In this tutorial, we will discuss why this error occurs and give you a quick and easy method of resolving it.”

Let’s get learning.

What is the “No Module Named NumPy” Error?

When you use the import keyword followed by the name of the module you wish to import, the Python interpreter will search the packages folder to ensure the package exists.

If the module is available, Python will proceed and evaluate the code as defined.

However, if Python cannot find the module in the package’s directory, it raises the ModuleNotFoundError, followed by the name of the package.

Therefore, if you see the “No Module Named NumPy” error, it means that the NumPy module is missing in your environment.

Causes of This Error

This error occurs if you do not have the NumPy package installed in your development environment.

How do I Resolve This Error?

To solve the “No Module Named NumPy” error, you need to install the NumPy package in your development environment.

You can do so by running the commands below:

Windows

$ pip install numpy

Before running pip to install numpy on Windows, ensure that the Python and pip binaries are available in your system’s path.

Linux & Mac

$ pip install numpy
$ pip3 install numpy

If you are using python2, use the pip command and pip3 for Python3.

Anaconda Environment/Jupyter/Spyder

If you are using the Anaconda distribution, Jupyter Lab/Notebook, or the Spyder IDE, you can install NumPy in your environment by running the command:

$ conda install numpy

Keep in mind that the command above is if you are using conda as your package manager.

NumPy in a Virtual Environment

Be aware when you are working in a virtual environment. For simplicity, Python virtual managers will attempt to keep your environment clean by including the minimalistic packages possible.

Therefore, even if you have NumPy installed on a global scope, you cannot access it in a virtual environment unless you install it in that environment.

You can use either pip or conda to install numpy in your target environment.

Check NumPy Package Info

If you are unsure whether NumPy is available in your environment, you can use the pip show command as:

$ pip show numpy
$ pip3 show numpy

If pip is installed in your environment, the command should return summary information of the NumPy package as shown below:

Closing

In this article, we learned the cause of the “ModuleNotFoundError: No module Named NumPy” error. We also provided possible solutions to resolve this error in your Python environment.

Thanks for reading & the Happy Problem Solving!!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list