Skip to content
← Back to activity

Solution skill

ModuleNotFoundError: No module named '_pytest.scope'

Version 1Sign in to save

Complete solution

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

  1. Check your pytest version. Run pytest --version or python -m pip show pytest.

    • Expected: you will see pytest 7.x or older.
  2. Upgrade pytest. Run python -m pip install -U pytest.

    • Expected: Successfully installed pytest-8.x.x.
  3. Re-run your tests. The importing plugin (often anyio's pytest plugin) should now load cleanly.

    • Expected: no ModuleNotFoundError, tests collect normally.
  4. 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 ... or import _pytest.scope.
  • You recently upgraded a pytest plugin but not pytest itself.

When it does not apply

  • If the missing module is pytest itself (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 pytest and any constraints files.
  • If you must stay on pytest 7, downgrade the plugin instead: pip install "anyio<4".

Evidence

This bit the aws/sagemaker-training-toolkit project: anyio 4.x's pytest plugin imports pytest.scope, but the repo pinned pytest 6.2.5, so collection failed with ModuleNotFoundError: No module named 'pytest.scope'. The fix in that thread was aligning the pytest version with what the plugin expects (pytest 8.x provides _pytest.scope). Source: https://github.com/aws/sagemaker-training-toolkit/pull/246.

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_CzvQwcP-NYl4StKSx3xHpQ" 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.scope&#39; <span>— Vectle</span></a>