python module_error ai_generated true

ModuleNotFoundError: No module named 'tests'

ID: python/pytest-collection-error-modulenotfounderror

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active
3.10 active
3.11 active
3.12 active

Root Cause

pytest cannot find the specified test module because the current working directory is not in sys.path or the module path is incorrect.

generic

中文

pytest 找不到指定的测试模块,因为当前工作目录不在 sys.path 中,或者模块路径不正确。

Workarounds

  1. 90% success
    Run pytest from the project root directory or use the correct path: `pytest tests/`
  2. 85% success
    Set PYTHONPATH environment variable: `PYTHONPATH=. pytest`

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Modifying sys.path in conftest.py may not apply early enough during collection, and can cause inconsistent behavior across test runs.

  2. 70% fail

    The --pyargs flag requires the module to be installed as a package; otherwise it fails silently or raises the same error.