{
  "id": "python/pytest-collection-import-error",
  "signature": "ERROR collecting tests/test_api.py\nImportError while importing test module 'tests/test_api.py'.\nModuleNotFoundError: No module named 'myapp'",
  "signature_zh": "收集 tests/test_api.py 时出错\n导入测试模块时 ImportError。\nModuleNotFoundError: 没有名为 'myapp' 的模块",
  "regex": "ERROR\\ collecting\\ tests/test_api\\.py\\\nImportError\\ while\\ importing\\ test\\ module\\ 'tests/test_api\\.py'\\.\\\nModuleNotFoundError:\\ No\\ module\\ named\\ 'myapp'",
  "domain": "python",
  "category": "install_error",
  "subcategory": null,
  "root_cause": "pytest adds the rootdir to sys.path only in certain modes. When invoked as 'pytest tests/' from a subdirectory or with a src layout, the package root is not on sys.path.",
  "root_cause_type": "generic",
  "root_cause_zh": "pytest 仅在特定模式下将 rootdir 加入 sys.path。当从子目录运行 'pytest tests/' 或使用 src 布局时，包根目录不在 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": "The CWD is not always the project root, and sys.path hacks break when tests run from CI with a different working directory.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This works only if the CWD is the project root; under src layout it still fails because the package lives under src/.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "pip install -e .\n# then run: pytest",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "# pytest.ini\n[pytest]\npythonpath = src",
      "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.88,
  "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": []
}