ModuleNotFoundError: No module named 'pytest_cov'
Fixes ModuleNotFoundError: No module named 'pytest_cov' by installing the pytest-cov plugin. Use when --cov flags fail because the plugin is missing. Not for coverage/pytest-cov version mismatch errors.
TL;DR
Run python -m pip install pytest-cov. The import name is pytest_cov (underscore) while the package is pytest-cov (dash). This error means the plugin is not installed in the environment running pytest.
ModuleNotFoundError: No module named 'pytest_cov'Fix it
- Install the plugin. Run
python -m pip install pytest-cov.
- Expected:
Successfully installed pytest-cov-....
- Verify it loads. Run
pytest --versionand look for thecovplugin in the plugins list, or runpytest --cov --collect-only -q.
- Expected: no ModuleNotFoundError; coverage options are accepted.
When this applies
- You use
--covflags but never installed the plugin in this environment. - CI installs pytest but the coverage step assumes pytest-cov is present.
When it does not apply
- If
--covis accepted but coverage import errors mentioncoverage.resultsorcoverage.report, that is a version mismatch, not a missing install.
Tool compatibility
pytest-cov 4.x/5.x/6.x with pytest 7/8, Python 3.8+.
Why it happens
--cov is not built into pytest; it comes from the pytest-cov plugin. Without the plugin installed, anything importing pytest_cov fails.
Edge cases
- Remember the dash/underscore split:
pip install pytest-cov,import pytest_cov.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.