Immutable solution version
ModuleNotFoundError: No module named 'pytest'
Complete solution
Fixes ModuleNotFoundError: No module named 'pytest' by installing pytest into the exact Python interpreter running the code. Use when pytest is not found at all. Not for plugin load failures or version-conflict import errors.
TL;DR
Run python -m pip install pytest, then verify with pytest --version. This error means pytest is not installed in the Python environment that is actually running your code, which is usually a different interpreter or virtualenv than the one you installed into.
ModuleNotFoundError: No module named 'pytest'Fix it
Confirm which Python is running. Run
which python(orwhere pythonon Windows) andpython --version. Then runpython -m pip show pytest. If pip says it is not installed, that is your answer.- Expected:
pip show pytestprints version info, orWARNING: Package(s) not found: pytest.
- Expected:
Install pytest into that exact interpreter. Run
python -m pip install pytest.- Expected:
Successfully installed pytest-...plus its dependencies.
- Expected:
Verify. Run
pytest --version.- Expected: something like
pytest 8.3.4.
- Expected: something like
When this applies
- You just created a fresh virtualenv or container and forgot to install test deps.
- Your IDE runs a different interpreter than your terminal.
- CI installs dependencies in one step but runs pytest in another without them.
When it does not apply
- If
pytest --versionworks but a plugin fails to load, the problem is the plugin, not pytest itself. - If the error names a different module (like
_pytest.scope), see the version-conflict skills instead.
Tool compatibility
pytest 7.x and 8.x, Python 3.8+. Install via pip: pip install pytest.
Why it happens
Python resolves imports against sys.path of the running interpreter. pip install pytest may have targeted a different interpreter (system python vs venv vs conda), so the running one never sees it. Using python -m pip guarantees the installer and the runner are the same binary.
Edge cases
- On Debian/Ubuntu,
pip install pytestmay refuse and tell you to use a venv (PEP 668). Create one:python3 -m venv .venv && source .venv/bin/activate. - If you use conda, prefer
conda install pytestinside the active env, orpython -m pip install pytestwith the env's python.
Evidence
People hit this when pytest is installed for one Python but tests run under another, the classic virtualenv mixup. The verified fix is to install with the running interpreter's own pip: python -m pip install pytest, then confirm with pytest --version. The pytest project documents installation via pip at https://docs.pytest.org/en/stable/getting-started.html, which is the canonical source for the correct install command.
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_cz2qEhJZnZSmva9CCkhmnw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">ModuleNotFoundError: No module named 'pytest' <span>— Vectle</span></a>