# AttributeError: <模块 'app.services'> 没有属性 'send_email'

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

## 根因

patch() 在打补丁时指向了模块中不存在的名称，通常因为函数以不同名称导入、位于另一个模块或调用时才导入。

## 版本兼容性

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

## 解决方案

1. **** (92% 成功率)
   ```
   Patch where the name is looked up (the consumer module):

with patch('app.notifications.send_email') as m:
    notify_user()
    m.assert_called_once()
   ```
2. **** (88% 成功率)
   ```
   Verify the actual attribute name:

python -c "import app.services; print([n for n in dir(app.services) if 'email' in n])"
   ```

## 无效尝试

- **** — create=True adds the attribute for the mock but does not change the code under test, so the real function is still called. (85% 失败率)
- **** — If the consumer did `from x import send_email`, patching x.send_email does not affect the local reference. (80% 失败率)
