Skip to content
Latchkey

python -m pytest: Run Tests with the Project on sys.path

Run pytest so your package imports actually resolve.

python -m pytest runs pytest as a module, which prepends the current directory to sys.path. This resolves many "module not found" collection errors that a bare pytest hits.

What it does

Runs the pytest module with the invoking interpreter and adds the working directory to sys.path (unlike the standalone pytest entry point). This makes local packages importable during test collection.

Common usage

Terminal
python -m pytest
python -m pytest tests/ -q
python -m pytest -k "auth and not slow" --maxfail=1

Common CI error: ModuleNotFoundError on collection

A bare "pytest" can fail to import your package because the project root is not on sys.path. Switching to "python -m pytest" (or installing the package with pip install -e .) fixes it.

Terminal
# Failure with bare pytest:
# ModuleNotFoundError: No module named 'myapp'

# Fix:
python -m pytest

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →