{
  "id": "python/unittest-mock-no-attribute",
  "signature": "AttributeError: module 'unittest' has no attribute 'mock'",
  "signature_zh": "AttributeError: 模块 'unittest' 没有属性 'mock'",
  "regex": "AttributeError:\\ module\\ 'unittest'\\ has\\ no\\ attribute\\ 'mock'",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "`unittest.mock` is a submodule that must be imported explicitly. Writing `import unittest` then `unittest.mock.patch(...)` fails because the submodule isn't loaded until imported.",
  "root_cause_type": "generic",
  "root_cause_zh": "`unittest.mock` 是子模块，必须显式导入。仅写 `import unittest` 后使用 `unittest.mock.patch(...)` 会失败，因为子模块未被加载。",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "mock has been a submodule since 3.3; version is not the problem, import style is.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Installing the backport doesn't bind `unittest.mock`; the attribute error persists.",
      "fail_rate": 0.65,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "Import the submodule explicitly:\nfrom unittest import mock\n# or\nimport unittest.mock\n\nwith mock.patch('mymodule.requests.get') as m:\n    ...",
      "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-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}