{
  "id": "python/pytest-import-mismatch",
  "signature": "ImportError: attempted relative import with no known parent package",
  "signature_zh": "ImportError: 尝试相对导入，但没有已知的父包",
  "regex": "ImportError:\\ attempted\\ relative\\ import\\ with\\ no\\ known\\ parent\\ package",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "A test file uses relative imports (e.g., from . import module) but is not part of a package, or is run as a script.",
  "root_cause_type": "generic",
  "root_cause_zh": "测试文件使用了相对导入（例如 from . import module），但它不是包的一部分，或作为脚本运行。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "If the test is run as a top-level script, relative imports still fail because there is no parent package.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This may work but can break if the package structure changes or if the test is run from different directories.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "From the project root, run: python -m pytest tests/\nThis ensures the current directory is in sys.path and relative imports work if tests are part of a package.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "pip install -e .\nThen use absolute imports like 'from mypackage import module' in 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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-09-01",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}