{
  "id": "python/pytest-collection-error-conftest-syntax",
  "signature": "ERROR collecting tests/test_app.py\nImportError while importing test module '/app/tests/test_app.py'.\nHint: make sure your test modules/packages have valid Python names.\nTraceback:\nconftest.py:5: in <module>\n    from myapp import settings\nE   ModuleNotFoundError: No module named 'myapp'",
  "signature_zh": "ERROR 收集 tests/test_app.py\n导入测试模块 '/app/tests/test_app.py' 时出现 ImportError。\n提示：确保你的测试模块/包具有有效的 Python 名称。\n回溯：\nconftest.py:5: in <module>\n    from myapp import settings\nE   ModuleNotFoundError: No module named 'myapp'",
  "regex": "ERROR\\ collecting\\ tests/test_app\\.py\\\nImportError\\ while\\ importing\\ test\\ module\\ '/app/tests/test_app\\.py'\\.\\\nHint:\\ make\\ sure\\ your\\ test\\ modules/packages\\ have\\ valid\\ Python\\ names\\.\\\nTraceback:\\\nconftest\\.py:5:\\ in\\ <module>\\\n\\ \\ \\ \\ from\\ myapp\\ import\\ settings\\\nE\\ \\ \\ ModuleNotFoundError:\\ No\\ module\\ named\\ 'myapp'",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "The project root is not on sys.path when pytest runs, so imports in conftest.py or test modules fail. This commonly happens when running pytest from a subdirectory or without installing the package in editable mode.",
  "root_cause_type": "generic",
  "root_cause_zh": "运行 pytest 时项目根目录不在 sys.path 上，导致 conftest.py 或测试模块中的导入失败。这通常发生在从子目录运行 pytest，或未以可编辑模式安装包时。",
  "versions": [
    {
      "version": "6.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "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": "Relative paths depend on CWD; running pytest from a different directory breaks it, and conftest.py is imported before test files so the fix is too late.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Works for interactive runs but is not persisted for CI or IDE test runners, leading to flaky 'works on my machine' behavior.",
      "fail_rate": 0.55,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "pip install -e .\n# or with pyproject.toml:\npip install -e '.[test]'",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "# pytest.ini or pyproject.toml\n[tool.pytest.ini_options]\npythonpath = ['.']\n# or CLI: pytest --import-mode=importlib",
      "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-06-11",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}