How to Solve "ModuleNotFoundError: No module named 'pytest'" in Python
The error ModuleNotFoundError: No module named 'pytest'
in Python means that you are trying to use the pytest
testing framework, but it is not installed in your current Python environment, or Python is unable to locate the installation.
This guide provides a step-by-step guide to installing pytest
, troubleshooting common installation problems, and configuring your IDE (VS Code, PyCharm) and Jupyter Notebook.
Installing pytest
(The Basic Solution)
The standard way to install pytest
is using pip
, Python's package installer:
pip install pytest
-
Multiple Python Versions: If you have multiple Python versions, you might need to use
pip3
(or a specific version likepip3.9
):pip3 install pytest
-
Permissions Errors: If you get a permissions error:
sudo pip3 install pytest # Linux/macOS (Use with caution!)
pip install pytest --user # Install for the current user onlyUsing virtual environments (explained below) is the best way to avoid permission problems.
-
python -m pip
: Ifpip
isn't working directly, try this more explicit form:python -m pip install pytest # Or python3 -m pip, or py -m pip
-
Conda: If you are using Anaconda, install
pytest
using:conda install -c anaconda pytest
Verifying the Installation
After installation, verify that pytest
is accessible. Create a simple test file (e.g., test_sample.py
):
# test_sample.py
def increment(x):
return x + 1
def test_answer():
assert increment(3) == 4
# Running the test
# pytest test_sample.py
- This is a minimal test file, used only to verify that
pytest
is installed correctly. - The test will fail on purpose, so that you get a quick feedback on whether the pytest module is installed correctly or not.
Then, run pytest
from your terminal in the same directory as test_sample.py
:
pytest test_sample.py
If pytest
is installed correctly, you'll see output similar to this (even though the test will fail, which is expected in this example):
=========================== test session starts ============================
platform ...
collected 1 item
test_sample.py F [100%]
================================= FAILURES =================================
________________________________ test_answer _________________________________
def test_answer():
> assert increment(3) == 5
E assert 4 == 5
E + where 4 = increment(3)
test_sample.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
======================== 1 failed, 1 warnings in 0.12s ========================
This output confirms that pytest
is installed and working. The test failure is expected; we're just verifying that pytest
itself can run. If you see a ModuleNotFoundError
, proceed to troubleshooting.
Troubleshooting ModuleNotFoundError
Virtual Environments (Essential)
Always use virtual environments:
-
Create:
python3 -m venv venv
(orpython -m venv venv
,py -m venv venv
) -
Activate:
- Linux/macOS:
source venv/bin/activate
- Windows (cmd):
venv\Scripts\activate.bat
- Windows (PowerShell):
venv\Scripts\Activate.ps1
- If PowerShell throws an error, you might need to enable script execution using
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
.
- If PowerShell throws an error, you might need to enable script execution using
- Linux/macOS:
-
Install:
pip install pytest
(inside the activated environment).
Your terminal prompt will change (e.g., (venv) $
).
Multiple Python Versions:
- Check:
python --version
(orpython3 --version
) - Correct
pip
: Use thepip
corresponding to your intended Python version.
IDE Configuration (VS Code, PyCharm)
Configure your IDE to use the correct interpreter/environment.
Jupyter Notebook
Install within a notebook cell:
!pip install pytest
Ensure your Jupyter kernel uses the right environment.
Naming Conflicts
Never name your files pytest.py
.
Reinstalling pytest
pip uninstall pytest
pip install pytest
Installation Instructions for Specific Environments
Windows:
- Open Command Prompt or PowerShell.
- Use
pip install pytest
(orpy -m pip install pytest
).
macOS / Linux:
- Open your Terminal
- Use
pip3 install pytest
Anaconda:
- Use
conda install -c anaconda pytest
Jupyter Notebook (within a cell):
!pip install pytest
Handling "Import 'pytest' could not be resolved" (Pylance - VS Code)
- Check interpreter in VS Code.
- Restart VS Code.
- Add comment to ignore warning if pytest is properly installed:
import pytest # type: ignore
Conclusion
The ModuleNotFoundError: No module named 'pytest'
error almost always indicates an installation or environment issue.
- Use virtual environments.
- Install with the correct
pip
command. - Configure your IDE/Jupyter properly.
- Avoid naming conflicts.
By following these steps, you can easily resolve the error and start using the powerful pytest
testing framework.