# PermissionError: [Errno 13] 权限被拒绝：'/tmp/pytest-of-root/pytest-0/test_write_file0/output.txt'

- **ID:** `python/pytest-tmp-path-permission-denied`
- **领域:** python
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

之前以 root 身份运行的 pytest 在 /tmp/pytest-of-<user>/ 下创建了由 root 拥有的 tmp_path 目录。随后以非 root 用户运行时无法写入这些目录。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   rm -rf /tmp/pytest-of-$(whoami)
# or, if owned by root:
sudo rm -rf /tmp/pytest-of-root
pytest tests/
   ```
2. **** (92% 成功率)
   ```
   pytest --basetemp=/home/$(whoami)/pytest-tmp tests/
# or in pyproject.toml:
# [tool.pytest.ini_options]
# basetemp = "/home/me/pytest-tmp"
   ```

## 无效尝试

- **** — Only fixes this run; the next pytest invocation creates a new numbered directory with the same root ownership problem. (80% 失败率)
- **** — pytest still uses /tmp/pytest-of-<user>, so the ownership collision persists. (85% 失败率)
