# AttributeError: <module 'myapp.services' from '/app/services.py'> 没有属性 'requests'

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

## 根因

unittest.mock.patch 针对的是名称被查找的位置，而非定义位置。若 services.py 使用 `import requests` 并调用 `requests.get`，应 patch 'myapp.services.requests.get'；若使用 `from requests import get`，则应 patch 'myapp.services.get'。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Identify the binding style in the SUT and patch accordingly:
  # if SUT: import requests
  @patch('myapp.services.requests')
  def test_get(mock_req): ...
  # if SUT: from requests import get
  @patch('myapp.services.get')
  def test_get(mock_get): ...
   ```
2. **** (90% 成功率)
   ```
   Use pytest-mock's mocker fixture which resolves the same lookup semantics:
  def test_get(mocker):
      mocker.patch('myapp.services.requests.get')
   ```

## 无效尝试

- **** — Patches the global requests module, not the reference the SUT uses; the real network call still fires. (75% 失败率)
- **** — Creates a separate binding; the SUT's binding remains untouched. (80% 失败率)
