pytest using the wrong Python / virtualenv (tested)

The pytest command runs with the interpreter in its own shebang line, which may be another virtualenv or the global Python. Check with which pytest and pytest -VV; fix by installing pytest into the active environment and running python -m pytest.

The pytest command is a small script whose first line names the Python interpreter it runs with. If the first pytest on your PATH belongs to a different virtualenv (or the global install), your tests run in that interpreter and cannot import packages from the one you activated. Below, the app virtualenv is active and has six installed, but pytest itself lives in another virtualenv, ve. Measured on pytest 9.1.1.

1. The symptom: ModuleNotFoundError for a package you did install

$ which python pytest
/tmp/wp384/app/bin/python
/tmp/fx384/ve/bin/pytest
$ head -1 "$(which pytest)"
#!/tmp/fx384/ve/bin/python
$ pytest -q | grep -E "^E |error"
E   ModuleNotFoundError: No module named 'six'
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
1 error in 0.07s

python is the app venv, but pytest resolves to ve/bin/pytest, whose shebang runs ve/bin/python. That interpreter has no six, so collection fails. pip list in the app venv shows six, which is why this is confusing.

2. See which interpreter pytest uses

$ pytest -VV 2>&1 | head -1
This is pytest version 9.1.1, imported from /tmp/fx384/ve/lib/python3.13/site-packages/pytest/__init__.py
$ pytest | grep platform
platform linux -- Python 3.13.5, pytest-9.1.1, pluggy-1.7.0

pytest -VV prints the path pytest was imported from, which tells you the environment. The normal header shows only the Python version, which looks identical when both environments use the same version. Inside a test, print(sys.executable) with -s shows the interpreter path (block 4).

3. python -m pytest uses the active interpreter

$ python -m pytest -q
/tmp/wp384/app/bin/python: No module named pytest

python -m pytest always runs with the python you call, so it either works or fails clearly with No module named pytest, telling you pytest is not installed in this environment.

4. Fix: install pytest into the same environment

$ python -m pip install -q pytest 2>/dev/null; echo installed
installed
$ hash -r; which pytest
/tmp/wp384/app/bin/pytest
$ pytest -q -s | head -2
/tmp/wp384/app/bin/python
.

After python -m pip install pytest in the active venv, its own bin/pytest comes first on PATH (run hash -r in bash, or open a new shell, if the old path is cached) and the test runs in the right interpreter. In VS Code, select the same interpreter (Python: Select Interpreter); in CI, call python -m pytest to remove the ambiguity entirely.

Related: pytest error collecting ModuleNotFoundError, pip error: externally-managed-environment.

Frequently asked questions

Why does pytest use the wrong Python?

The first pytest on PATH belongs to another environment, and its shebang line runs that environment's interpreter. Install pytest in your active venv or use python -m pytest.

How do I see which Python pytest is using?

Run pytest -VV (shows where pytest was imported from) or print(sys.executable) inside a test and run with -s.

Why does python -m pytest say No module named pytest?

pytest is not installed in that interpreter. Run python -m pip install pytest with the same python.

Written with AI assistance. Measured on 2026-10-23 with pytest 9.1.1 on Python 3.13.5, two virtualenvs on Linux. Each block was run verbatim and the output pasted unedited. PlainHeaders is our extension; Python and pytest are free.