Skip to main content

How to Solve "ModuleNotFoundError: No module named 'numpy'" in Python

The error ModuleNotFoundError: No module named 'numpy' in Python indicates that the NumPy library is not installed in the Python environment you're using, or that Python can not locate the installation.

This guide explains how to install NumPy, troubleshoot common installation issues, and configure various development environments (VS Code, PyCharm, Jupyter Notebook, Anaconda).

Installing NumPy (The Basic Solution)

The standard way to install NumPy is using pip, Python's package installer. Open your terminal or command prompt and run:

pip install numpy
  • Multiple Python Versions: If you have multiple Python versions, you might need to use pip3 (or even a more specific version, like pip3.9 or pip3.10) to ensure you're installing into the correct environment:

    pip3 install numpy
  • Permissions Errors: If you get a permissions error (common on Linux/macOS), try installing into a user site-packages directory (avoids needing sudo):

    pip install numpy --user
    #or
    pip3 install numpy --user
    • Using virtual environments (explained below) avoids permission issues and is highly recommended.
  • python -m pip: If pip isn't directly in your PATH, use:

    python -m pip install numpy  # Or python3 -m pip, py -m pip

    This ensures you're using the pip associated with your current Python interpreter.

  • Conda If you are using Anaconda, you can install the package:

    conda install -c anaconda numpy

Verifying the Installation

After installing, verify that NumPy is accessible by running a simple Python script:

import numpy as np

a = np.arange(6)
a2 = a[np.newaxis, :]
print(a2.shape) # Output: (1, 6)

If this runs without a ModuleNotFoundError, NumPy is installed correctly. If you still get the error, proceed to the troubleshooting steps.

Troubleshooting ModuleNotFoundError

If you're still encountering the error after installation, here are the common problems and their solutions:

Virtual Environments (Essential)

Always use virtual environments for your Python projects. This isolates your project's dependencies and prevents conflicts.

  1. Create a virtual environment (if you don't have one):

    python3 -m venv venv  # Or python -m venv venv , or py -m venv venv
  2. Activate the environment:

    • Linux/macOS: source venv/bin/activate
    • Windows (cmd.exe): venv\Scripts\activate.bat
    • Windows (PowerShell): venv\Scripts\Activate.ps1
      • If you see an error that ps1 can not be loaded because running scripts is disabled on this system, run the following command, type "yes" when prompted and rerun the activation command.
        Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
  3. Install NumPy inside the activated environment:

    pip install numpy

Your terminal prompt should change (e.g., (venv) $). This indicates the environment is active. Now run your Python script.

Multiple Python Versions

You might have installed NumPy for a different Python version than the one your script is using.

  • Check your Python version: python --version (or python3 --version)
  • Use the correct pip: Use the pip (or pip3, pip3.x, python -m pip) that corresponds to the Python version you're using.

IDE Configuration (VS Code, PyCharm)

Your IDE (VS Code, PyCharm, etc.) must be configured to use the correct Python interpreter (and virtual environment, if applicable).

  • VS Code:

    1. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
    2. Type "Python: Select Interpreter".
    3. Choose the interpreter associated with your project's virtual environment (it will usually have the environment name in parentheses). If you don't see it, choose "Enter interpreter path..." and browse to the python or python.exe executable inside your venv directory.
  • PyCharm:

    1. Go to "File" -> "Settings" (or "PyCharm" -> "Preferences" on macOS) -> "Project" -> "Python Interpreter".
    2. Select the correct interpreter from the dropdown, or click the gear icon to add/configure a new one. Make sure to select the interpreter within your virtual environment.

Jupyter Notebook

In Jupyter Notebook, you can install the package by using:

!pip install numpy
note

This installs numpy into the environment that Jupyter itself is running in, which may not be your project's environment. For reliable results, ensure your Jupyter kernel is using the correct environment.

Naming Conflicts

Never name your own Python files or directories numpy.py or numpy. This creates a naming conflict, and Python will try to import your file instead of the installed numpy library. Rename your file.

Reinstalling NumPy

As a last resort, try uninstalling and reinstalling NumPy:

pip uninstall numpy
pip install numpy

Installation Instructions for Specific Environments

Windows:

  • Open Command Prompt or PowerShell.
  • Use pip install numpy (or py -m pip install numpy).

macOS / Linux:

  • Open your Terminal
  • Use pip3 install numpy.

Anaconda:

  • Use Anaconda Navigator, or conda install -c anaconda numpy

Jupyter Notebook (within a cell):

!pip install numpy

Handling "Import 'numpy' could not be resolved from source Pylance" (VS Code Specific)

This Pylance-specific error (in VS Code) almost always means your interpreter is not set correctly in VS Code. Selecting the correct interpreter and restarting VS Code can also help.

If that does not work, as a last resort, you can add # type: ignore next to your import statement to tell the linter to ignore that line.

import numpy as np  # type: ignore
  • This approach is not recommended, and should only be used to ignore the Pylance warnings, and only if you are sure that the package is available in your current environment.

Conclusion

The ModuleNotFoundError: No module named 'numpy' error is almost always an installation or environment issue.

By systematically working through the steps above (verifying installation, using virtual environments, checking IDE settings, and avoiding naming conflicts) you can resolve this error and use NumPy effectively.

Remember, virtual environments are essential for managing dependencies and preventing these kinds of problems.