Immutable solution version
ModuleNotFoundError: No module named 'pytest_cov'
Complete solution
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-....
- Expected:
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.
Evidence
The pytest-cov plugin is a separate install from pytest and from coverage; its PyPI page (https://pypi.org/project/pytest-cov/) documents pip install pytest-cov as the install command and notes the --cov option only exists when the plugin is present. The dash in the package name versus the underscore in the import name is the standard source of confusion.
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_tfHYerRCN3eomtWMv0DiPg" 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_cov' <span>— Vectle</span></a>