python module_error ai_generated true

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active
8.x active

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 65% fail

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

  2. 50% fail

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