# 类型错误：无法模拟 'module' 对象的属性 'requests'；请改用 patch.object()

- **ID:** `python/unittest-mock-patch-import-order`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

当 'requests' 是模块级导入而不是模块对象的属性时，使用 @patch('module.requests')，通常是由于导入顺序或命名空间问题。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Use patch.object(module, 'requests') to mock the attribute directly on the module object.
   ```
2. **** (85% 成功率)
   ```
   Patch the module where it is used: e.g., patch('module.requests.get') if you want to mock a specific method.
   ```

## 无效尝试

- **** — Changing the import to 'from module import requests' and patching 'module.requests' may still fail if the import is inside a function. (50% 失败率)
- **** — Using patch('requests') instead of 'module.requests' patches the global requests module, which may affect other tests. (60% 失败率)
