Linux and Unix
On Linux, the BSDs and any other platform that is not Windows, macOS, Android or iOS, PlatformDirs is platformdirs.unix.Unix. It follows the XDG Base Directory Specification and the xdg-user-dirs convention. This page lists every directory with its default and override, then explains the runtime directory, root redirection and the safety checks that are specific to Unix.
Examples use appname="SuperApp"; appauthor is ignored on Unix. With version="1.0", every app directory gains a trailing /1.0 (before /log for logs).
User Directories
| Property | Default | Override |
|---|---|---|
user_data_dir |
~/.local/share/SuperApp |
$XDG_DATA_HOME/SuperApp |
user_config_dir |
~/.config/SuperApp |
$XDG_CONFIG_HOME/SuperApp |
user_preference_dir |
same as user_config_dir |
|
user_cache_dir |
~/.cache/SuperApp |
$XDG_CACHE_HOME/SuperApp |
user_state_dir |
~/.local/state/SuperApp |
$XDG_STATE_HOME/SuperApp |
user_log_dir |
~/.local/state/SuperApp/log |
$XDG_STATE_HOME/SuperApp/log; without /log if opinion=False |
user_runtime_dir |
/run/user/<uid>/SuperApp |
$XDG_RUNTIME_DIR/SuperApp if valid; see below |
user_documents_dir |
~/Documents |
XDG_DOCUMENTS_DIR, user-dirs.dirs |
user_downloads_dir |
~/Downloads |
XDG_DOWNLOAD_DIR, user-dirs.dirs |
user_pictures_dir |
~/Pictures |
XDG_PICTURES_DIR, user-dirs.dirs |
user_videos_dir |
~/Videos |
XDG_VIDEOS_DIR, user-dirs.dirs |
user_music_dir |
~/Music |
XDG_MUSIC_DIR, user-dirs.dirs |
user_desktop_dir |
~/Desktop |
XDG_DESKTOP_DIR, user-dirs.dirs |
user_projects_dir |
~/Projects |
XDG_PROJECTS_DIR, user-dirs.dirs |
user_publicshare_dir |
~/Public |
XDG_PUBLICSHARE_DIR, user-dirs.dirs |
user_templates_dir |
~/Templates |
XDG_TEMPLATES_DIR, user-dirs.dirs |
user_fonts_dir |
~/.local/share/fonts |
$XDG_DATA_HOME/fonts |
user_bin_dir |
~/.local/bin |
none |
user_applications_dir |
~/.local/share/applications |
$XDG_DATA_HOME/applications |
~ is expanded from HOME, or from the password database when HOME is unset, empty or relative. If no absolute home is available, every property under ~ raises RuntimeError: could not determine the home directory for '~/...', set an absolute HOME or XDG variable. Site directories and absolute XDG values still work in that situation.
Site Directories
| Property | Default | Override |
|---|---|---|
site_data_dir |
/usr/local/share/SuperApp (with multipath: /usr/local/share/SuperApp:/usr/share/SuperApp) |
$XDG_DATA_DIRS entries + /SuperApp |
site_config_dir |
/etc/xdg/SuperApp |
$XDG_CONFIG_DIRS entries + /SuperApp |
site_cache_dir |
/var/cache/SuperApp |
none |
site_state_dir |
/var/lib/SuperApp |
none |
site_log_dir |
/var/log/SuperApp (opinion has no effect) |
none |
site_runtime_dir |
/run/SuperApp (/var/run/SuperApp on FreeBSD, OpenBSD, NetBSD) |
if XDG_RUNTIME_DIR is set: same as user_runtime_dir |
site_bin_dir |
/usr/local/bin |
none |
site_applications_dir |
/usr/local/share/applications (with multipath: plus /usr/share/applications) |
$XDG_DATA_DIRS entries + /applications |
site_applications_dir and site_bin_dir never include appname or version.
site_runtime_dir and XDG_RUNTIME_DIR
When XDG_RUNTIME_DIR holds an absolute path, site_runtime_dir returns exactly what user_runtime_dir returns (including its validation and fallback), because the specification ties that variable to the logged-in user. Only when the variable is unset does it return the system path /run/SuperApp. If you specifically want root's per-user runtime directory (/run/user/0/...), use user_runtime_dir.
The Runtime Directory
user_runtime_dir is the most carefully checked directory, because runtime directories hold sockets and lock files that other users must not be able to hijack. The resolution goes through three candidates:
-
$XDG_RUNTIME_DIR, if it is an absolute path that passes all checks:- it is a real directory, not a symlink;
- it is owned by the effective user;
- its mode is exactly
0700; - every parent (including parents reached through symlinks) is owned by root or the effective user, and is not writable by group or others unless the sticky bit is set.
A missing
XDG_RUNTIME_DIRpasses whenensure_exists=False; withensure_exists=Trueplatformdirs creates it with mode0700and checks again. -
The platform default
/run/user/<uid>(/var/run/user/<uid>on FreeBSD and NetBSD,/tmp/run/user/<uid>on OpenBSD), if it exists and passes the same checks. - A temporary fallback
runtime-<uid>inside the temporary directory (for example/tmp/runtime-1000).
Whenever the variable is unset or rejected, platformdirs emits a platformdirs.RuntimeDirWarning naming the cause and the fallback chosen, once per distinct message per process:
RuntimeDirWarning: XDG_RUNTIME_DIR is not set, falling back to /run/user/1000
RuntimeDirWarning: XDG_RUNTIME_DIR /run/user/1000 has mode 0755, not 0700, falling back to /tmp/runtime-1000
(The exact paths and problem descriptions come from the checks above; other problem texts include is a symlink, is not a directory, is owned by uid N, not M and is below <parent>, writable by another user.)
The Temporary Fallback
The last-resort directory has stricter rules, because /tmp is shared:
- Its base is taken from
TMPDIR,TEMPorTMP(absolute values only), then/tmp,/var/tmp,/usr/tmp. If Python's own temporary directory is not one of those, platformdirs probes the candidates for a writable one; if none works it raisesPermissionError: no safe temporary directory; set XDG_RUNTIME_DIR. - The base's parents must pass the runtime checks.
- If
runtime-<uid>is a symlink, not a directory, or owned by another user, readinguser_runtime_dirraisesPermissionError: runtime directory /tmp/runtime-1000 is owned by uid 1001, not 1000; set XDG_RUNTIME_DIR instead(or similar). - A lookup without
ensure_existsaccepts an existing directory that is group-writable but not world-writable. Withensure_exists=True, platformdirs creates it with0700and resets the mode of an existing one to0700(older releases created it as0755). - Python caches its temporary directory in
tempfile.tempdir; setting that attribute alone cannot pick a custom base, so useTMPDIR.
Silencing the Warning
Headless environments (CI, containers, cron) often have no login session and therefore no XDG_RUNTIME_DIR. If your test suite turns warnings into errors, either set XDG_RUNTIME_DIR to a private directory you create with mode 0700, or filter the warning:
import warnings
import platformdirs
warnings.filterwarnings("ignore", category=platformdirs.RuntimeDirWarning)
With pytest you can do the same in configuration:
[pytest]
filterwarnings =
ignore::platformdirs.RuntimeDirWarning
Media Directories and user-dirs.dirs
Each media property checks, in order, the XDG_*_DIR environment variable (absolute values only), the user-dirs.dirs file in $XDG_CONFIG_HOME or ~/.config, and finally the default under the home directory. The parsing and trust rules for the file are described in Environment Variables.
With ensure_exists=True, a media directory is created (mode 0775 minus the umask) only when its location came from the variable or the file, never for a default like ~/Documents. A configured path that is a dangling symlink, which happens with unmounted network or removable storage, is returned unchanged instead of failing.
Running as Root
By default root is treated like any user, so user_data_dir is /root/.local/share/SuperApp. With use_site_for_root=True, an effective UID of 0 redirects the user_* properties to the site ones:
Property as root with use_site_for_root=True |
Result |
|---|---|
user_data_dir |
/usr/local/share/SuperApp |
user_config_dir |
/etc/xdg/SuperApp |
user_cache_dir |
/var/cache/SuperApp |
user_state_dir |
/var/lib/SuperApp |
user_log_dir |
/var/log/SuperApp |
user_runtime_dir |
/run/SuperApp |
user_bin_dir |
/usr/local/bin |
user_applications_dir |
/usr/local/share/applications |
user_documents_dir and other media, user_fonts_dir |
default under the absolute HOME, else root's home from the password database |
XDG user variables, XDG_RUNTIME_DIR and user-dirs.dirs are bypassed for these, because under sudo they usually still describe the invoking user.
Creating site directories as root is checked: root can look up an existing directory owned by a service account, but creating a directory (with ensure_exists) or placing a file (with place_*_file) requires every path component to be owned by root. Group-writable ancestors such as /var/log at 0775 are allowed. New directories get mode 0755 minus the umask.
Directory Creation Checks for Normal Users
When ensure_exists or place_*_file() creates a user directory, platformdirs walks the existing part of the path first and refuses (PermissionError) to create anything below a component that another user could swap out:
- In a group-writable parent without the sticky bit, entries may belong to you, root, or the parent's owner.
- World-writable directories require the sticky bit.
- Shared sticky directories, entries inside them and symlinks must belong to root or you.
The details are in Directory Creation and Safety.
Termux and Other Android Shells
On Android, Termux, Pydroid and similar apps that provide a Linux shell set SHELL or PREFIX. platformdirs detects this and uses this Unix backend, so all XDG variables work there. See Android.
Pyodide and Other Platforms Without geteuid
UID-dependent logic (runtime directory names, ownership checks, root detection) uses os.geteuid(). Where that function does not exist, as in Pyodide, os.getuid() is used instead.