# AttributeError: <module 'app.services.user'> does not have the attribute 'send_email'

- **ID:** `python/pytest-mock-patch-wrong-target`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

`patch('app.services.user.send_email')` fails when the name is imported into the module under test from elsewhere. Patch must target the name *where it is looked up* (the consumer module), not where it is defined.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.10 | active | — | — |
| 3.12 | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   Patch where the name is used:

from unittest.mock import patch

@patch('app.services.user.send_email')
def test_signup(mock_send):
    ...
   ```
2. **** (85% success)
   ```
   Change the import in the production module to a module-level reference and patch the module:

# app/services/user.py
from app import notifications
notifications.send_email(...)

# test
patch('app.services.user.notifications.send_email')
   ```

## Dead Ends

- **** — The module under test already bound the name at import time; patching the definition site does not affect the bound reference. (80% fail)
- **** — Creates a fake attribute on the module, but the code under test still calls the original binding, so the mock is never invoked. (75% fail)
