{
  "id": "python/pytest-import-file-mismatch",
  "signature": "import file mismatch:\n  /proj/tests/test_a.py has __pycache__ ...\n  imported module 'test_a' has this __file__ attribute:\n    /proj/tests/sub/test_a.py",
  "signature_zh": "导入文件不匹配：\n  /proj/tests/test_a.py 具有 __pycache__ ...\n  导入的模块 'test_a' 具有此 __file__ 属性：\n    /proj/tests/sub/test_a.py",
  "regex": "import\\ file\\ mismatch:\\\n\\ \\ /proj/tests/test_a\\.py\\ has\\ __pycache__\\ \\.\\.\\.\\\n\\ \\ imported\\ module\\ 'test_a'\\ has\\ this\\ __file__\\ attribute:\\\n\\ \\ \\ \\ /proj/tests/sub/test_a\\.py",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "Two test files share the same basename in different directories without __init__.py, so Python's module cache collides on the module name 'test_a'.",
  "root_cause_type": "generic",
  "root_cause_zh": "两个测试文件在不同目录下具有相同的基本名称，且没有 __init__.py，导致 Python 模块缓存在模块名 'test_a' 上发生冲突。",
  "versions": [
    {
      "version": "7.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Cache regeneration recreates the same collision on the next run.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Hash randomization is unrelated to module name resolution.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "May work short-term but the same pattern recurs as the suite grows.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "touch tests/__init__.py tests/sub/__init__.py\n# then rerun: pytest -q",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "# pytest.ini\n[pytest]\naddopts = --import-mode=importlib",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "mv tests/test_a.py tests/test_alpha.py\nmv tests/sub/test_a.py tests/sub/test_a_sub.py",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}