How to Install pytest in Python
pytest: simple powerful testing with Python
pip install pytest
What is pytest?
pytest: simple powerful testing with Python
The `` framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
# content of testsample.py def inc(x): return x + 1
def testanswer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3)
Quick Start
Minimal example to get started with pytest:
# test_math.py
def add(x, y):
return x + y
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
def test_add_strings():
assert add("a", "b") == "ab"
# Run: pytest test_math.py -v
Installation
pip (standard)
pip install pytest
Virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install pytest
pip3
pip3 install pytest
conda
conda install -c conda-forge pytest
Poetry
poetry add pytest
Dependencies
Installing pytest will also install these packages:
Verify the Installation
After installing, confirm the package is available:
python -c "import pytest; print(pytest.__version__)"
If this prints a version number, installation succeeded. If you see a ModuleNotFoundError, see the errors section below.
Installation Errors
Common errors when installing pytest with pip.
ModuleNotFoundError: No module named 'pytest'
Cause: The package is not installed in the current Python environment.
Fix: Run pip install pytest. If using a virtual environment, ensure it is activated first.
ModuleNotFoundError: No module named 'pytest' (installed but still failing)
Cause: pip installed the package into a different Python than the one running your script.
Fix: Use python -m pip install pytest to install into the interpreter you are running.
ImportError: cannot import name 'X' from 'pytest'
Cause: The function or class does not exist in the installed version.
Fix: Check the version with pip show pytest and upgrade with pip install --upgrade pytest.
pip: command not found
Cause: pip is not in PATH or Python was not added to PATH during installation.
Fix: Try python -m pip install pytest. On macOS/Linux try pip3.
PermissionError: [Errno 13] Permission denied
Cause: No write access to the system Python package directory.
Fix: Use a virtual environment, or add --user: pip install --user pytest
SSL: CERTIFICATE_VERIFY_FAILED
Cause: pip cannot verify PyPI's SSL certificate — common behind corporate proxies.
Fix: Try: pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org pytest
Runtime Errors
Common errors when using pytest after installation.
fixture 'X' not found
Cause: Fixture not in scope, misspelled, or the plugin providing it is not installed.
Fix: Define the fixture in conftest.py. For plugin fixtures (e.g., pytest-asyncio), install the plugin.
ImportError while collecting test file
Cause: pytest cannot import the test module.
Fix: Run python -c 'import your_module' to reproduce the error directly.
Recent Releases
| Version | Released |
|---|---|
9.0.3 latest |
2026-04-07 |
9.0.2 |
2025-12-06 |
9.0.1 |
2025-11-12 |
9.0.0 |
2025-11-08 |
8.4.2 |
2025-09-04 |
Manage pytest
Upgrade to latest version
pip install --upgrade pytest
Install a specific version
pip install pytest==9.0.3
Uninstall
pip uninstall pytest
Check what is installed
pip show pytest