Environment Variables

platformdirs has no configuration file of its own. Users and administrators influence the returned paths through environment variables (and, on Linux, the user-dirs.dirs file). This page lists every variable the library reads, per platform, with the exact validation rules, so you know which overrides your users can apply and why some values are silently ignored.

All variables are read on every property access; nothing is cached. Setting os.environ["XDG_CACHE_HOME"] inside your program takes effect on the next read.

XDG Base Directory Variables

Read on Linux/Unix and macOS (not on Windows, Android or iOS), following the XDG Base Directory Specification.

Variable Overrides Linux default macOS default
XDG_DATA_HOME user_data_dir, user_fonts_dir ($XDG_DATA_HOME/fonts), user_applications_dir ($XDG_DATA_HOME/applications) ~/.local/share ~/Library/Application Support
XDG_CONFIG_HOME user_config_dir (and user_preference_dir on Linux) ~/.config ~/Library/Application Support
XDG_CACHE_HOME user_cache_dir ~/.cache ~/Library/Caches
XDG_STATE_HOME user_state_dir (and user_log_dir on Linux) ~/.local/state ~/Library/Application Support
XDG_RUNTIME_DIR user_runtime_dir, site_runtime_dir /run/user/<uid> ~/Library/Caches/TemporaryItems
XDG_DATA_DIRS site_data_dir, site_applications_dir /usr/local/share:/usr/share /Library/Application Support (Homebrew prefix first)
XDG_CONFIG_DIRS site_config_dir /etc/xdg /Library/Application Support (Homebrew prefix first)
export XDG_CONFIG_HOME="$HOME/.myconfig"
python -c "from platformdirs import user_config_dir; print(user_config_dir('MyApp'))"
/home/<user>/.myconfig/MyApp

Validation Rules

  • Only absolute paths count. A value that does not start with / is ignored as if unset. That includes ~/cache, cache, and a value with leading whitespace before the /.
  • Whitespace is kept. A trailing space or tab in an accepted value becomes part of the directory name.
  • List variables (XDG_DATA_DIRS, XDG_CONFIG_DIRS) are split on :; relative entries are dropped, and if no absolute entry remains, the default list is used.
  • XDG_RUNTIME_DIR is additionally checked for ownership and permissions; see Linux and Unix.
  • With use_site_for_root active (root on Unix), the *_HOME variables and XDG_RUNTIME_DIR are bypassed for redirected directories.

macOS Specifics

The same variables work on macOS and take precedence over the ~/Library defaults. Some consequences are easy to miss:

  • user_log_dir and user_preference_dir on macOS are not affected by any XDG variable.
  • user_state_dir follows XDG_STATE_HOME even though its default is the data directory.
  • Setting XDG_DATA_HOME moves user_fonts_dir to $XDG_DATA_HOME/fonts and user_applications_dir to $XDG_DATA_HOME/applications, away from ~/Library/Fonts and ~/Applications.
  • Setting XDG_DATA_DIRS changes site_applications_dir from /Applications to <entry>/applications.
  • site_state_dir ignores XDG_DATA_DIRS.

See macOS.

XDG User Directory Variables and user-dirs.dirs

These control the media directories.

Variable Property
XDG_DOCUMENTS_DIR user_documents_dir
XDG_DOWNLOAD_DIR user_downloads_dir
XDG_PICTURES_DIR user_pictures_dir
XDG_VIDEOS_DIR user_videos_dir
XDG_MUSIC_DIR user_music_dir
XDG_DESKTOP_DIR user_desktop_dir
XDG_PROJECTS_DIR user_projects_dir
XDG_PUBLICSHARE_DIR user_publicshare_dir
XDG_TEMPLATES_DIR user_templates_dir

Note the singular XDG_DOWNLOAD_DIR.

Resolution order:

  1. Environment variable, if it holds an absolute path (Linux/Unix and macOS).
  2. user-dirs.dirs file (Linux/Unix only), the file written by xdg-user-dirs-update, at $XDG_CONFIG_HOME/user-dirs.dirs, or ~/.config/user-dirs.dirs when XDG_CONFIG_HOME is unset or relative.
  3. Platform default such as ~/Documents.

How user-dirs.dirs Is Parsed

The file contains shell-style assignments:

XDG_DOCUMENTS_DIR="$HOME/Documents"
XDG_DOWNLOAD_DIR="$HOME/Downloads"
XDG_PROJECTS_DIR="/data/projects"

platformdirs parses it like the xdg-user-dir utility:

  • Blanks are allowed around the key and after =. Later valid lines for the same key win.
  • A value must be $HOME, start with $HOME/, or be an absolute path. Other values are skipped.
  • Inside double quotes, a backslash is removed only before $, a backtick, " or another backslash; other backslashes stay literal. Outside quotes, a backslash is removed before any character, so $HOME/Reports\ #2026 names Reports #2026 rather than starting a comment. Text after a closing quote is ignored.
  • Percent signs are literal ($HOME/100% complete works).
  • A trailing carriage return is dropped, so files with Windows line endings work.
  • Undecodable bytes are preserved using the file system encoding with surrogateescape.

When the File Is Ignored

platformdirs only trusts a user-dirs.dirs that another user cannot have planted. The file is ignored (and defaults apply) when:

  • it is missing or unreadable;
  • it is not a regular file, is owned by someone other than root or you, or is writable by group or others;
  • its directory, or any parent, is owned by someone other than root or you, or is world-writable without the sticky bit (a group-writable ~/.config at 0775 is fine);
  • it is a symlink that is not owned by root or you, or whose target's parents fail the same check.

There is no warning when this happens. If your media directories unexpectedly show defaults, check the permissions with ls -l ~/.config/user-dirs.dirs.

Windows: WIN_PD_OVERRIDE_*

Windows has no standard per-application override mechanism, so platformdirs checks its own variables before asking Windows. The name is WIN_PD_OVERRIDE_ followed by the folder's CSIDL name without CSIDL_:

Variable Folder it replaces Affects
WIN_PD_OVERRIDE_APPDATA AppData\Roaming user_data_dir (and config, state, preference, log) with roaming=True
WIN_PD_OVERRIDE_LOCAL_APPDATA AppData\Local user_data_dir family, user_cache_dir, user_runtime_dir (<value>\Temp), user_fonts_dir (<value>\Microsoft\Windows\Fonts)
WIN_PD_OVERRIDE_COMMON_APPDATA C:\ProgramData site_data_dir, site_config_dir, site_cache_dir, site_state_dir, site_log_dir, site_bin_dir (<value>\bin)
WIN_PD_OVERRIDE_PERSONAL Documents user_documents_dir
WIN_PD_OVERRIDE_DOWNLOADS Downloads user_downloads_dir
WIN_PD_OVERRIDE_MYPICTURES Pictures user_pictures_dir
WIN_PD_OVERRIDE_MYVIDEO Videos user_videos_dir
WIN_PD_OVERRIDE_MYMUSIC Music user_music_dir
WIN_PD_OVERRIDE_DESKTOPDIRECTORY Desktop user_desktop_dir
WIN_PD_OVERRIDE_PROGRAMS Start Menu Programs user_applications_dir
WIN_PD_OVERRIDE_COMMON_PROGRAMS All Users Start Menu Programs site_applications_dir
WIN_PD_OVERRIDE_TEMPLATES Templates user_templates_dir
WIN_PD_OVERRIDE_PUBLIC C:\Users\Public user_publicshare_dir
WIN_PD_OVERRIDE_USER_PROGRAM_FILES AppData\Local\Programs user_bin_dir

A typical use is moving large caches (models, package downloads) to another drive without changing LOCALAPPDATA, which other programs depend on:

$env:WIN_PD_OVERRIDE_LOCAL_APPDATA = "D:\appdata"
python -c "from platformdirs import user_cache_dir; print(user_cache_dir('MyApp', 'Acme'))"
D:\appdata\Acme\MyApp\Cache

Validation rules for override values:

  • Leading whitespace and trailing ASCII whitespace are stripped. Other trailing whitespace, such as a no-break space, stays and becomes part of the folder name.
  • The value must be fully qualified: a drive and a root (D:\appdata), a UNC path with both server and share (\\server\share\appdata), or a device path such as \\?\D:\appdata. A blank value, a rooted path without a drive (\shared), a drive-relative path (D:appdata), a UNC path missing the server or share (\\server), or one using . or .. as server or share name is ignored, and the normal folder is used.

There is no override for the Projects folder; user_projects_dir is always <home>\Projects, where the home comes from USERPROFILE, else HOMEDRIVE plus HOMEPATH. HOME is ignored on Windows.

Home and Temporary Directory Variables

Variable Platforms Used for
HOME Linux/Unix, macOS, iOS Expanding ~. If unset, empty or relative, the password database entry of the current user is used. If neither gives an absolute path, any directory under the home raises RuntimeError.
USERPROFILE, HOMEDRIVE, HOMEPATH Windows Expanding ~ for user_projects_dir, and the environment-variable fallback for media folders.
APPDATA, LOCALAPPDATA, ALLUSERSPROFILE, PROGRAMDATA, PUBLIC Windows Only used by the last-resort resolver when neither ctypes nor winreg is importable (with winreg but no ctypes, PUBLIC and LOCALAPPDATA still supply the Public and user Programs folders).
TMPDIR, TEMP, TMP Linux/Unix Choosing the base of the last-resort runtime directory, <tmp>/runtime-<uid>. Only absolute values are considered.

Android Detection Variables

Variable Effect
ANDROID_DATA=/data together with ANDROID_ROOT=/system Marks the process as running on Android, in addition to sys.getandroidapilevel() existing.
SHELL or PREFIX (any non-empty value) Marks an Android shell environment such as Termux or Pydroid; platformdirs then uses the Unix/XDG backend instead of the Android one.

See Android.

Precedence Summary

For a user app directory on Linux, from highest to lowest priority:

  1. use_site_for_root redirect (root only), which ignores everything below;
  2. the XDG variable, if absolute (and, for the runtime directory, valid);
  3. the built-in default under HOME (or the password database home).

For a media directory on Linux: redirect for root, then XDG_*_DIR, then user-dirs.dirs, then the default.

For any Windows directory: WIN_PD_OVERRIDE_* if fully qualified, then the Known Folder API, then the registry, then environment variables.

See Also