ModuleNotFoundError: No module named 'scipy' (Python) — how to fix
Traceback (most recent call last):
File "analysis.py", line 2, in <module>
from scipy import stats
ModuleNotFoundError: No module named 'scipy'
Quick fix: Run python -m pip install scipy with the same interpreter (activate your venv first), or conda install scipy in conda. Verify with python -c "import scipy; print(scipy.__version__)". If pip tries to build from source, upgrade pip first: python -m pip install --upgrade pip.
What it means
Python searched sys.path and found no scipy package. Installing NumPy does not install SciPy; it's a separate package. If scikit-learn raises this error, your scikit-learn install is broken (SciPy is one of its dependencies) — reinstall both.
Fix 1: install it with the interpreter that runs your code
python -m pip install scipy
python -c "import scipy; print(scipy.__version__)"
Using python -m pip guarantees the package goes into the same Python as python. A bare pip can belong to a different installation. Compare:
python -c "import sys; print(sys.executable)"
python -m pip --version
Fix 2: use a virtual environment
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install --upgrade pip
python -m pip install numpy scipy
On recent Debian/Ubuntu, the system Python refuses pip install (externally-managed-environment). Either use a venv, or install the distro package: sudo apt install python3-scipy.
Fix 3: pip tries to build SciPy from source and fails
If install output shows Building wheel for scipy followed by errors about Fortran, gfortran, meson, or OpenBLAS, pip didn't find a prebuilt wheel. Causes:
- Old pip that doesn't understand modern wheel tags — run
python -m pip install --upgrade pip. - Brand-new Python with no SciPy wheels yet — use the previous Python version, or wait for the next SciPy release.
- 32-bit Python or an unusual platform (e.g. Alpine/musl in older SciPy versions) — use 64-bit Python, or a
python:3.x-slimDocker image instead of Alpine.
Check whether a wheel exists for your setup:
python -m pip download scipy --only-binary=:all: --no-deps -d /tmp/x
Fix 4: Jupyter or IDE uses another kernel
In a notebook:
%pip install scipy
Restart the kernel afterwards. In VS Code pick the right interpreter via "Python: Select Interpreter"; in PyCharm install it under Settings → Project → Python Interpreter.
Fix 5: conda
conda activate myenv
conda install scipy
Don't mix conda install numpy with pip install scipy in the same environment if you can avoid it; mismatched BLAS builds can crash at import.
Fix 6: shadowing and partial installs
- A local file named
scipy.pyor a folder namedscipy/hides the real package — rename it. ImportError: cannot import name ... from scipyor errors about_ufuncsafter an interrupted install: reinstall cleanly:
python -m pip uninstall -y scipy
python -m pip install --no-cache-dir scipy
Import tip
SciPy subpackages must usually be imported explicitly:
import scipy
scipy.stats # works on SciPy 1.9+ (lazy loading), AttributeError on older versions
from scipy import stats # works everywhere
Last updated 2026-11-16.