python module_error ai_generated true

导入文件不匹配: tests/test_a.py 有 __pycache__ 但 tests/test_a.py 有不同的 __pycache__ 提示:删除 __pycache__ 文件或使用 --import-mode=importlib

import file mismatch: tests/test_a.py has __pycache__ but tests/test_a.py has different __pycache__ HINT: remove __pycache__ files or use --import-mode=importlib

ID: python/pytest-import-mode-conflict

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

版本兼容性

版本状态引入弃用备注
7.x active
8.x active

根因分析

两个测试文件共享相同的模块基名(例如 tests/api/test_a.py 和 tests/db/test_a.py),且两个目录都没有 __init__.py,因此 pytest 的 prepend 导入模式在模块名上冲突。

English

Two test files share the same module basename (e.g. tests/api/test_a.py and tests/db/test_a.py) and neither directory has an __init__.py, so pytest's prepend import mode collides on the module name.

generic

解决方案

  1. 95% 成功率
    # pytest.ini
    [pytest]
    importmode = importlib
  2. 90% 成功率
    touch tests/__init__.py tests/api/__init__.py tests/db/__init__.py

无效尝试

常见但无效的做法:

  1. 65% 失败

    Clears the symptom for one run; the next run recreates the collision.

  2. 50% 失败

    Works but does not scale; the next same-named file reintroduces the problem.