python module_error ai_generated true

ModuleNotFoundError: 没有名为 'tests' 的模块

ModuleNotFoundError: No module named 'tests'

ID: python/pytest-collection-error-modulenotfounderror

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-03-15首次发现

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active
3.10 active
3.11 active
3.12 active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 60% 失败

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

  2. 70% 失败

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