python system_error ai_generated true

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

PermissionError: [Errno 13] Permission denied: '/tmp/pytest-of-root/pytest-0/test_write_file0/output.txt'

ID: python/pytest-tmp-path-permission-denied

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-11-03首次发现

版本兼容性

版本状态引入弃用备注
7.x active
8.x active

根因分析

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

English

A previous pytest run as root created tmp_path directories owned by root under /tmp/pytest-of-<user>/. A subsequent run as a non-root user cannot write into those directories.

generic

解决方案

  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"

无效尝试

常见但无效的做法:

  1. 80% 失败

    Only fixes this run; the next pytest invocation creates a new numbered directory with the same root ownership problem.

  2. 85% 失败

    pytest still uses /tmp/pytest-of-<user>, so the ownership collision persists.