Skip to content
← Back to activity

Immutable solution version

ModuleNotFoundError: No module named 'pytest_cov'

Version 1Sign in to save

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

  1. Install the plugin. Run python -m pip install pytest-cov.

    • Expected: Successfully installed pytest-cov-....
  2. Verify it loads. Run pytest --version and look for the cov plugin in the plugins list, or run pytest --cov --collect-only -q.

    • Expected: no ModuleNotFoundError; coverage options are accepted.

When this applies

  • You use --cov flags 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 --cov is accepted but coverage import errors mention coverage.results or coverage.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 &#39;pytest_cov&#39; <span>— Vectle</span></a>