How to Fix ModuleNotFoundError: No module named 'numpy' During pip Install
This guide helps resolve an issue where a Python package fails to install because it cannot find numpy
, even though it is already installed in your virtual environment.
Problem
You’re trying to install a Python package using:
uv pip install git+https://github.com/example/examplepkg.git
# or
pip install .
Even though numpy
is installed in your environment, the install fails with:
ModuleNotFoundError: No module named 'numpy'
Why This Happens
By default, pip (and uv pip) use build isolation when installing packages that declare a build backend in pyproject.toml. This means they build the package in a temporary, isolated environment with only the dependencies listed in the [build-system]
table of pyproject.toml
.
If numpy
is required during the build (e.g. to compile C extensions) but not listed in build-system.requires
, it will not be available, leading to the error.
Solution
Use the --no-build-isolation
flag to bypass the isolated environment and allow access to already-installed packages:
pip install --no-build-isolation .
# or
uv pip install --no-build-isolation git+https://github.com/example/examplepkg.git
--no-build-isolation
tells pip to use your current environment, where numpy
is already installed. This avoids the problem entirely, but at the cost of potentially inconsistent builds.Permanent Fix: Add Build Requirements
A better long-term solution is to ensure that required build dependencies like numpy
are declared in pyproject.toml
, following PEP 518:
[build-system]
requires = ["setuptools", "wheel", "numpy"]
build-backend = "setuptools.build_meta"
This allows pip to correctly build the package in an isolated environment with the necessary dependencies.
[build-system.requires]
to ensure compatibility with modern Python packaging tools.