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

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

## 根因

unittest.mock 是一个子模块，必须显式导入；仅 `import unittest` 不会暴露 `unittest.mock`，且在某些环境中名为 unittest.py 的本地文件会遮蔽标准库。

## 版本兼容性

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

## 解决方案

1. **** (98% 成功率)
   ```
   from unittest import mock
# or
from unittest.mock import patch, MagicMock

with patch('module.func') as m:
    m.return_value = 42
   ```
2. **** (90% 成功率)
   ```
   python -c "import unittest; print(unittest.__file__)"
# If it prints a path inside your project, rename that file (e.g. unittest_local.py).
   ```

## 无效尝试

- **** — If a local unittest.py shadows the stdlib, this imports the local file and still lacks .mock. (50% 失败率)
- **** — Star import does not import submodules and is discouraged; mock is still not bound. (80% 失败率)
