{
  "id": "python/pytest-collection-error-importerror",
  "signature": "ERROR collecting tests/test_api.py\nImportError while importing test module 'tests/test_api.py'.\nHint: make sure your test modules/packages have valid Python names.\nModuleNotFoundError: No module named 'myapp'",
  "signature_zh": "收集 tests/test_api.py 时出错\n导入测试模块 'tests/test_api.py' 时发生 ImportError。\n提示: 确保你的测试模块/包具有有效的 Python 名称。\nModuleNotFoundError: 没有名为 'myapp' 的模块",
  "regex": "ERROR\\ collecting\\ tests/test_api\\.py\\\nImportError\\ while\\ importing\\ test\\ module\\ 'tests/test_api\\.py'\\.\\\nHint:\\ make\\ sure\\ your\\ test\\ modules/packages\\ have\\ valid\\ Python\\ names\\.\\\nModuleNotFoundError:\\ No\\ module\\ named\\ 'myapp'",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "pytest is invoked from a directory where the project root is not on sys.path, and the package is not installed in editable mode; pytest's rootdir-based sys.path insertion does not cover the source layout.",
  "root_cause_type": "generic",
  "root_cause_zh": "pytest 从项目根目录不在 sys.path 上的目录运行，且包未以可编辑模式安装；pytest 基于 rootdir 的 sys.path 插入不覆盖源码布局。",
  "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": "Works locally but breaks in CI where cwd differs; also fails when tests are collected before the insert executes for other modules.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Does not persist across CI jobs or IDE runs; developers forget to export it and the error returns.",
      "fail_rate": 0.65,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.97,
      "how": "pip install -e .\n# or with pyproject:\npip install -e '.[dev]'\npytest",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.94,
      "how": "# pyproject.toml\n[tool.pytest.ini_options]\npythonpath = [\".\"]\ntestpaths = [\"tests\"]",
      "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.9,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-02-09",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}