{
  "id": "python/pytest-import-mode-sys-path",
  "signature": "ModuleNotFoundError: No module named 'myapp'\n  File \"/app/tests/test_api.py\", line 3, in <module>\n    from myapp.api import create_app",
  "signature_zh": "ModuleNotFoundError：没有名为 'myapp' 的模块\n  文件 \"/app/tests/test_api.py\"，第 3 行，在 <module> 中\n    from myapp.api import create_app",
  "regex": "ModuleNotFoundError:\\ No\\ module\\ named\\ 'myapp'\\\n\\ \\ File\\ \"/app/tests/test_api\\.py\",\\ line\\ 3,\\ in\\ <module>\\\n\\ \\ \\ \\ from\\ myapp\\.api\\ import\\ create_app",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "The project root is not on sys.path when pytest runs. This happens when tests are invoked from a directory other than the project root, or when the package is not installed (pip install -e .) and there is no conftest.py at the project root to anchor rootdir.",
  "root_cause_type": "generic",
  "root_cause_zh": "pytest 运行时项目根目录不在 sys.path 上。当从项目根目录以外的目录调用测试，或者包未安装（pip install -e .）且项目根目录没有 conftest.py 来锚定 rootdir 时，就会发生这种情况。",
  "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": "Depends on CWD at runtime; breaks in CI when pytest is invoked from a different directory, and pollutes each test file.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Works interactively but not in CI or IDE test runners; teammates hit the same error.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.97,
      "how": "pip install -e .\npytest tests/",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "# project/conftest.py (empty is enough)\ntouch conftest.py\npytest tests/",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "# pyproject.toml\n[tool.pytest.ini_options]\npythonpath = [\".\"]",
      "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-01-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}