{
  "id": "python/unittest-mock-not-found",
  "signature": "ModuleNotFoundError: No module named 'mock'",
  "signature_zh": "ModuleNotFoundError: 没有名为 'mock' 的模块",
  "regex": "ModuleNotFoundError:\\ No\\ module\\ named\\ 'mock'",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "Code imports the standalone backport `mock` package, but it is not installed (or the project targets Python 3 where unittest.mock should be used). The backport was only needed for Python 2 or very old Python 3.",
  "root_cause_type": "generic",
  "root_cause_zh": "代码导入了独立的 `mock` 后端包，但未安装（或项目面向 Python 3，应使用 unittest.mock）。该后端包仅在 Python 2 或很旧的 Python 3 中才需要。",
  "versions": [
    {
      "version": "3.8",
      "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": "The test then runs without any mocking, so the real network or DB is hit and tests fail or hang for a different reason.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Old mock versions are incompatible with modern patch targets and pytest, causing AttributeError or patching failures.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.97,
      "how": "Replace the import with the stdlib module:\n\n# from mock import patch  (remove)\nfrom unittest.mock import patch, MagicMock\n\nOn Python 3.3+ this requires no installation.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "If legacy code must keep `import mock`, install the backport: `pip install mock` and add it to requirements-dev.txt.",
      "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.89,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-17",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}