ModuleNotFoundError: No module named '_pytest.scope'
Fixes ModuleNotFoundError: No module named '_pytest.scope' by upgrading pytest to 8.x, which introduced that internal module. Use when a pytest plugin imports _pytest.scope on an old pytest. Not for a missing pytest install.
TL;DR
Upgrade pytest to 8.x: python -m pip install -U pytest. The _pytest.scope module was added in pytest 8.0, so any plugin or tool importing it (like newer anyio) breaks on pytest 7.x and older with this exact error.
ModuleNotFoundError: No module named '_pytest.scope'Fix it
- Check your pytest version. Run
pytest --versionorpython -m pip show pytest.
- Expected: you will see pytest 7.x or older.
- Upgrade pytest. Run
python -m pip install -U pytest.
- Expected:
Successfully installed pytest-8.x.x.
- Re-run your tests. The importing plugin (often anyio's pytest plugin) should now load cleanly.
- Expected: no ModuleNotFoundError, tests collect normally.
- If you cannot upgrade pytest, pin the plugin to a version that supports your pytest. For anyio, that means staying on anyio 3.x with pytest 7.x.
When this applies
- The traceback shows a plugin (anyio, or similar) doing
from _pytest.scope import ...orimport _pytest.scope. - You recently upgraded a pytest plugin but not pytest itself.
When it does not apply
- If the missing module is
pytestitself (not_pytest.scope), pytest is simply not installed. - If the error names a hook (like
pytest_namespace), that is a plugin validation error, a different problem.
Tool compatibility
pytest 8.0+ provides _pytest.scope. anyio 4.x requires it. Python 3.8+.
Why it happens
_pytest.scope is an internal pytest module introduced in pytest 8.0. Plugins written against pytest 8 import it at load time. If your environment pins pytest 7.x (common in CI images or libraries with upper bounds), the import fails even though everything else works.
Edge cases
- Some distributions pin pytest via constraints files; check
pip freeze | grep -i pytestand any constraints files. - If you must stay on pytest 7, downgrade the plugin instead:
pip install "anyio<4".
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.