{
  "id": "python/unittest-testloader-discover-error",
  "signature": "ImportError: Start directory 'tests' is not importable: 'tests' is not a package",
  "signature_zh": "导入错误：起始目录 'tests' 不可导入：'tests' 不是一个包",
  "regex": "ImportError:\\ Start\\ directory\\ 'tests'\\ is\\ not\\ importable:\\ 'tests'\\ is\\ not\\ a\\ package",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "When using unittest.TestLoader().discover('tests'), the directory must be a Python package (containing __init__.py) and importable from the current Python path.",
  "root_cause_type": "generic",
  "root_cause_zh": "使用 unittest.TestLoader().discover('tests') 时，该目录必须是 Python 包（包含 __init__.py），并且可以从当前 Python 路径导入。",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Adding sys.path.append('.') may fix the import but is a fragile workaround that can cause other import issues.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Changing the discovery pattern (e.g., 'test*.py') without fixing the package structure still fails if the directory is not a package.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Add an empty __init__.py file to the 'tests' directory to make it a package, then run discovery from the parent directory.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.8,
      "how": "Use a relative path or run the test script from the parent directory of 'tests' to ensure proper import resolution.",
      "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.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-07-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}