python system_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-11-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active
8.x active

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 85% fail

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