python
system_error
ai_generated
true
PermissionError: [Errno 13] Permission denied: '/tmp/pytest-of-user/pytest-123/test_xxx0/tmp_file.txt'
ID: python/pytest-tmp-path-permission
80%Fix Rate
82%Confidence
0Evidence
2024-08-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.4.0 | active | — | — | — |
| 8.0.0 | active | — | — | — |
Root Cause
测试尝试写入 tmp_path 目录,但该目录权限受限,或测试以非特权用户运行。
generic中文
测试尝试写入 tmp_path 目录,但该目录权限受限,或测试以非特权用户运行。
Workarounds
-
90% success
在测试中打印 tmp_path 并确保目录存在且可写,必要时使用 `tmp_path.mkdir(parents=True, exist_ok=True)`。
-
85% success
使用 `tmp_path_factory.mktemp('test_dir')` 创建自定义目录,确保权限正确。
Dead Ends
Common approaches that don't work:
-
70% fail
修改系统目录权限可能影响其他进程,且不安全。
-
60% fail
绝对路径可能指向不可写位置,且测试不可移植。