How to Solve "ModuleNotFoundError: No module named 'scipy'" in Python
The ModuleNotFoundError: No module named 'scipy'
error means that Python can't find the scipy
library. This usually happens because SciPy isn't installed, is installed in the wrong environment, or your IDE/Jupyter isn't configured correctly.
This guide will show you how to install SciPy and troubleshoot common installation problems.
Installing SciPy (The Basic Solution)
The standard way to install SciPy is using pip
:
pip install scipy
-
Multiple Python Versions: If you have multiple Python versions, you may need
pip3
(or a more specific version):pip3 install scipy
-
Permissions Errors: If you get a permissions error, try:
sudo pip3 install scipy # Linux/macOS (Use with caution!)
pip install scipy --user # Installs for the current userUsing virtual environments (explained below) is the best way to avoid permission issues.
-
python -m pip
: Ifpip
isn't directly accessible, use this:python -m pip install scipy # Or python3 -m pip
This ensures you are using the pip that corresponds to your Python interpreter.
-
Anaconda: If you're using Anaconda, it's generally best to use
conda
:conda install -c anaconda scipy
Verifying the Installation
After installing, verify the installation with a simple Python script:
from scipy import stats
from scipy.stats import norm
print(dir(stats)) # Prints a list of names defined in the module.
print('bounds of distribution lower: %s, upper: %s' % norm.support())
# Output: bounds of distribution lower: -inf, upper: inf
If this code runs without a ModuleNotFoundError
, SciPy is installed and available. If you still get the error, proceed to the troubleshooting section.
Troubleshooting ModuleNotFoundError
Virtual Environments (Essential)
Always use virtual environments for your Python projects. This isolates project dependencies, prevents conflicts, and is crucial for reproducibility.
-
Create a virtual environment (if you haven't already):
python3 -m venv venv # Or python -m venv venv , py -m venv venv
-
Activate the environment:
- Linux/macOS:
source venv/bin/activate
- Windows (cmd.exe):
venv\Scripts\activate.bat
- Windows (PowerShell):
venv\Scripts\Activate.ps1
- You might get error when running
venv\Scripts\Activate.ps1
. In this case run the commandSet-ExecutionPolicy RemoteSigned -Scope CurrentUser
in powershell and try again.
- You might get error when running
- Linux/macOS:
-
Install SciPy inside the activated environment:
pip install scipy
Your terminal prompt should now show the active environment (e.g., (venv) $
). Now try running your script.
Multiple Python Versions
Make sure you installed SciPy for the correct Python version.
- Check your Python version:
python --version
(orpython3 --version
) - Use the correct
pip
: Use thepip
(orpip3
,pip3.x
, orpython -m pip
) that corresponds to the Python version you are using.
IDE Configuration (VS Code, PyCharm)
Your IDE (VS Code, PyCharm, etc.) needs to be configured to use the correct Python interpreter (and the correct virtual environment).
-
VS Code:
- Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
- Type "Python: Select Interpreter".
- Choose the interpreter associated with your virtual environment.
-
PyCharm:
- Go to "File" -> "Settings" -> "Project" -> "Python Interpreter".
- Select the correct interpreter from the dropdown (or add it if it's not listed).
Jupyter Notebook
In a Jupyter Notebook cell, install with:
!pip install scipy
- The
!
is used to execute a terminal command within a notebook cell.
This installs SciPy into the environment Jupyter itself is running in. Make sure your Jupyter kernel is using the same environment as your project.
Naming Conflicts
Never name your own files or directories scipy.py
or scipy
. Python might try to import your file/directory instead of the installed SciPy library. Rename your file/directory.
Reinstalling SciPy
As a last resort, try uninstalling and reinstalling:
pip uninstall scipy
pip install scipy
Installation Instructions for Specific Environments
Windows:
- Open Command Prompt or PowerShell.
- Use
pip install scipy
(orpy -m pip install scipy
).
macOS / Linux:
- Open your Terminal
- Use
pip3 install scipy
Anaconda:
- Use
conda install -c anaconda scipy
Jupyter Notebook (within a cell):
!pip install scipy
Import Errors After Successful Installation
Even if pip install scipy
succeeds, you might still get import errors within your code if you aren't importing SciPy correctly. The following import statements are valid:
import scipy
: imports the top-level SciPy package. You'll need to usescipy.submodule.function
to access functions.from scipy import submodule
: imports a specific submodule (e.g.,from scipy import stats
). You can then usestats.function
.from scipy.submodule import function
: imports a specific function (e.g.,from scipy.stats import norm
). You can then usefunction
directly.
The code example below imports stats
from scipy
and uses the cdf
method for cumulative distribution:
from scipy import stats
from scipy.stats import norm
print(stats.norm.cdf(0)) # Output: 0.5
Conclusion
The ModuleNotFoundError: No module named 'scipy'
error almost always indicates an installation or environment issue. The key is to:
- Use virtual environments.
- Install SciPy using the correct
pip
for your Python version. - Ensure your IDE or Jupyter kernel is using the correct environment.
- Avoid naming conflicts.
By following these steps, you should be able to resolve the error and use SciPy successfully.