ImportError: cannot import name 'display_covered' from 'coverage.results'
Fixes ImportError for display_covered from coverage.results by upgrading pytest-cov and coverage together. Use when old pytest-cov meets new coverage. Not for a missing pytest-cov install.
TL;DR
Align your coverage and pytest-cov versions: python -m pip install -U pytest-cov coverage. Old pytest-cov imports display_covered from coverage.results, which newer coverage versions removed.
ImportError: cannot import name 'display_covered' from 'coverage.results'Fix it
- Check versions. Run
python -m pip show pytest-cov coverage.
- Expected: you will see an old pytest-cov (for example 3.x/4.x) next to a new coverage (7.x).
- Upgrade both together. Run
python -m pip install -U pytest-cov coverage.
- Expected:
Successfully installed pytest-cov-6.x coverage-7.x(or current).
- Re-run. The plugin import should succeed now.
- Expected:
pytest --covstarts without the ImportError.
When this applies
- The traceback points at
pytest_cov/plugin.pyline doingfrom coverage.results import display_covered. - You pinned one of the two packages and let the other float.
When it does not apply
- If the error is about
coverage.report.Reporter, that is a different removed name. - If pytest-cov is not installed at all, install it first.
Tool compatibility
pytest-cov 5+ with coverage 7.x. Python 3.8+.
Why it happens
pytest-cov reaches into coverage's internals. When coverage refactors those internals (removing display_covered), old pytest-cov releases break. Upgrading pytest-cov gets the fixed import.
Edge cases
- If you must pin coverage (for example
coverage==6.5.0), pin a contemporary pytest-cov instead (4.x era).
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.