# AttributeError: 模块 'unittest' 没有属性 'mock'

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

## 根因

`unittest.mock` 是子模块，必须显式导入。仅写 `import unittest` 后使用 `unittest.mock.patch(...)` 会失败，因为子模块未被加载。

## 版本兼容性

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

## 解决方案

1. **** (98% 成功率)
   ```
   Import the submodule explicitly:
from unittest import mock
# or
import unittest.mock

with mock.patch('mymodule.requests.get') as m:
    ...
   ```

## 无效尝试

- **** — mock has been a submodule since 3.3; version is not the problem, import style is. (80% 失败率)
- **** — Installing the backport doesn't bind `unittest.mock`; the attribute error persists. (65% 失败率)
