{
  "id": "python/unittest-module-has-no-attribute-mock",
  "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; `import unittest` alone does not expose `unittest.mock`, and in some environments a local file named unittest.py shadows the stdlib.",
  "root_cause_type": "generic",
  "root_cause_zh": "unittest.mock 是一个子模块，必须显式导入；仅 `import unittest` 不会暴露 `unittest.mock`，且在某些环境中名为 unittest.py 的本地文件会遮蔽标准库。",
  "versions": [
    {
      "version": "3.8",
      "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": "If a local unittest.py shadows the stdlib, this imports the local file and still lacks .mock.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Star import does not import submodules and is discouraged; mock is still not bound.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "from unittest import mock\n# or\nfrom unittest.mock import patch, MagicMock\n\nwith patch('module.func') as m:\n    m.return_value = 42",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "python -c \"import unittest; print(unittest.__file__)\"\n# If it prints a path inside your project, rename that file (e.g. unittest_local.py).",
      "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.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-20",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}