# ModuleNotFoundError: 没有名为 'mock' 的模块

- **ID:** `python/unittest-mock-not-found`
- **领域:** python
- **类别:** module_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

代码导入了独立的 `mock` 后端包，但未安装（或项目面向 Python 3，应使用 unittest.mock）。该后端包仅在 Python 2 或很旧的 Python 3 中才需要。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (97% 成功率)
   ```
   Replace the import with the stdlib module:

# from mock import patch  (remove)
from unittest.mock import patch, MagicMock

On Python 3.3+ this requires no installation.
   ```
2. **** (90% 成功率)
   ```
   If legacy code must keep `import mock`, install the backport: `pip install mock` and add it to requirements-dev.txt.
   ```

## 无效尝试

- **** — The test then runs without any mocking, so the real network or DB is hit and tests fail or hang for a different reason. (85% 失败率)
- **** — Old mock versions are incompatible with modern patch targets and pytest, causing AttributeError or patching failures. (80% 失败率)
