{
  "id": "python/unittest-mock-not-callable-import",
  "signature": "AttributeError: <module 'unittest' from '.../unittest/__init__.py'> has no attribute 'mock'",
  "signature_zh": "AttributeError: <module 'unittest' from '.../unittest/__init__.py'> 没有属性 'mock'",
  "regex": "AttributeError:\\ <module\\ 'unittest'\\ from\\ '\\.\\.\\./unittest/__init__\\.py'>\\ has\\ no\\ attribute\\ 'mock'",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "The test uses `import unittest` and then `unittest.mock.patch`, but `unittest.mock` is not automatically imported as an attribute. It must be imported explicitly.",
  "root_cause_type": "generic",
  "root_cause_zh": "测试使用 `import unittest` 后调用 `unittest.mock.patch`，但 `unittest.mock` 不会自动作为属性导入，必须显式导入。",
  "versions": [
    {
      "version": "3.9",
      "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": "unittest is part of the standard library; installing a PyPI package named 'unittest' either fails or installs an unrelated shim that shadows the stdlib.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Modifying the stdlib is fragile and the change is overwritten on interpreter upgrade.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.97,
      "how": "Import mock explicitly:\n\nimport unittest\nfrom unittest import mock\n\nclass TestX(unittest.TestCase):\n    @mock.patch('mymodule.func')\n    def test_it(self, m):\n        ...",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.97,
      "how": "Or import the submodule directly:\n\nfrom unittest.mock import patch, MagicMock",
      "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-18",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}