Troubleshooting

This page collects the errors, warnings and surprising paths people run into with platformdirs, with their cause and fix. Start by running python -m platformdirs (Command Line Report) on the affected machine: it shows every resolved directory and prints errors in place instead of stopping.

RuntimeDirWarning: XDG_RUNTIME_DIR is not set, falling back to ...

Where: Linux and other Unix systems, usually in CI, Docker containers, cron jobs or su/sudo sessions without a login session.

Cause: user_runtime_dir (or site_runtime_dir, iter_runtime_dirs, place_runtime_file) was read and XDG_RUNTIME_DIR is missing. platformdirs falls back to /run/user/<uid> or a private /tmp/runtime-<uid> and warns once per distinct message.

A variant names a rejected value instead:

RuntimeDirWarning: XDG_RUNTIME_DIR /run/user/1000 has mode 0755, not 0700, falling back to /tmp/runtime-1000

Fix:

  • If the fallback is fine for you, filter the warning: warnings.filterwarnings("ignore", category=platformdirs.RuntimeDirWarning), or in pytest configuration filterwarnings = ignore::platformdirs.RuntimeDirWarning.
  • Otherwise create a private directory and point the variable at it:

    mkdir -m 700 -p "/tmp/xdg-runtime-$(id -u)"
    export XDG_RUNTIME_DIR="/tmp/xdg-runtime-$(id -u)"
  • If the variant says has mode ..., is owned by uid ... or is below ..., writable by another user, fix the permissions of that directory or its parents. See The Runtime Directory.

On macOS the warning appears only when XDG_RUNTIME_DIR is set but unusable; an unset variable silently uses ~/Library/Caches/TemporaryItems.

PermissionError: runtime directory ...; set XDG_RUNTIME_DIR instead

Cause: the last-resort runtime directory <tmp>/runtime-<uid> exists but is a symlink, not a directory, or owned by another user (someone may have created it first), or the temporary directory's parents are unsafe. Without ensure_exists, an existing fallback that is world-writable is rejected too.

Fix: set XDG_RUNTIME_DIR to a private directory you own (see above), or set TMPDIR to a private temporary directory. If you own the existing runtime-<uid>, ensure_exists=True resets its mode to 0700.

PermissionError: no safe temporary directory; set XDG_RUNTIME_DIR means none of TMPDIR, TEMP, TMP, /tmp, /var/tmp and /usr/tmp is usable.

RuntimeError: could not determine the home directory

RuntimeError: could not determine the home directory for '~/.config', set an absolute HOME or XDG variable

Cause: on a POSIX system, HOME is unset, empty or relative, and the password database has no absolute home for the current user. This happens in minimal containers that run as an arbitrary UID with no /etc/passwd entry, and in some sandboxes.

Fix: set HOME to an absolute path, or set the XDG variable for the directory you use (XDG_CONFIG_HOME, XDG_DATA_HOME, XDG_CACHE_HOME, XDG_STATE_HOME), which avoids the home lookup for that directory.

A related message, could not determine the home directory, refusing to create '~/...', comes from ensure_exists or place_*_file, which refuse to create a literal ~ folder in the current directory. On Windows, the same RuntimeError can come from user_projects_dir when USERPROFILE, HOMEDRIVE and HOMEPATH are all unset.

ValueError: appname must stay inside the base directory

Cause: appname, appauthor or version (or the name passed to place_*_file / find_*_file) is an absolute path, contains a drive such as C:, or has a .. component. platformdirs refuses because os.path.join would otherwise leave the base directory.

Fix: pass a plain name. Subfolders are allowed ("Acme/SuperApp"), escaping is not. See Path Validation of Names.

ValueError: name must point to a file

Cause: place_*_file("") or place_*_file(".").

Fix: pass the relative path of the file to create, for example place_config_file("config.toml").

ValueError: multipath cannot join site directories that contain ':'

Cause: multipath=True with an appname or version that contains os.pathsep (: on POSIX). The joined string would be ambiguous.

Fix: remove the : from the name, or use the iterators, which return the entries separately.

PermissionError: ..., owned by uid N When Creating Directories

Cause: with ensure_exists=True or place_*_file(), platformdirs found a path component another user could tamper with: a directory owned by someone else inside a shared folder, a world-writable directory without the sticky bit, or a symlink owned by another user. Other variants are ..., writable by another user, ..., which is not a directory and ... is not a trusted directory.

Fix: correct the ownership or permissions of the named path, or point the relevant XDG variable elsewhere. The rules are in Directory Creation and Safety.

PermissionError: site cache directory /Library/Caches/... is owned by uid ...

Cause: macOS only. site_cache_dir with ensure_exists=True found an entry in the world-writable /Library/Caches that belongs to neither root nor you.

Fix: remove the foreign entry (as an administrator) or avoid ensure_exists for site_cache_dir.

RuntimeError: cannot find the Android app folder

Cause: you constructed platformdirs.android.Android directly, but neither pyjnius nor sys.path revealed the app's package folder. The automatic PlatformDirs selection only picks the Android class when the folder is found, so this happens only with direct use.

Fix: use PlatformDirs, or make sure the app's files directory is on sys.path. See Android.

The App Name Appears Twice on Windows

user_data_dir("SuperApp") returns ...\AppData\Local\SuperApp\SuperApp. When appauthor is None, Windows uses the app name as the author folder. Pass an author, or appauthor=False for ...\AppData\Local\SuperApp. See The Author Folder.

Cache and Log Paths Have the Version in Different Places on Windows

With version="1.0", the cache is ...\SuperApp\Cache\1.0 but logs are ...\SuperApp\1.0\Logs. This is long-standing behavior inherited from appdirs, not a bug. See The Opinion Subfolders.

Files Are Not Where the Path Says (Microsoft Store Python)

With Python from the Microsoft Store, Windows redirects new folders under AppData to a per-package location. Other programs then do not find your files at the logical path. Resolve the real location with os.path.realpath() after creating the directory. See Microsoft Store Python.

An XDG Variable Is Ignored

  • Relative values are ignored. XDG_CONFIG_HOME=config counts as unset; use an absolute path.
  • Not every directory reads every variable. On macOS, user_log_dir and user_preference_dir ignore XDG variables. On Linux, logs follow XDG_STATE_HOME, not XDG_CACHE_HOME.
  • use_site_for_root=True as root bypasses user XDG variables on purpose.
  • Windows ignores XDG variables; use WIN_PD_OVERRIDE_* instead.

See Environment Variables.

user-dirs.dirs Is Ignored

On Linux, platformdirs reads media folders from ~/.config/user-dirs.dirs only if the file and its folder are trusted (owned by you or root, not writable by others) and the file is well-formed. Otherwise it silently uses the defaults. The full rules are in When the File Is Ignored.

Different Paths Under sudo

sudo usually keeps the invoking user's HOME and XDG variables, so root may compute paths inside your home and create root-owned files there. For services and installers that run as root, use use_site_for_root=True, which switches to system locations and ignores those variables. See Running as Root.

Data and Config Overwrite Each Other on macOS

user_data_dir, user_config_dir and user_state_dir are the same folder on macOS (and iOS). Use distinct file names or subfolders. See macOS.

Linux Is Detected on Android

In Termux, Pydroid and similar apps, SHELL or PREFIX is set, so platformdirs deliberately uses the Unix backend and XDG paths under the shell's home. See Android.

site_applications_dir("SuperApp") Behaves Oddly

site_applications_dir takes multipath as its first positional parameter, so the string sets multipath. Pass names by keyword: site_applications_dir(appname="SuperApp"). See Module Functions.

Tests Write to My Real Home Directory

Use the platformdirs_isolated fixture or platformdirs.testing.isolated_dirs(). Note that a spawned subprocess does not inherit the isolation; enter isolated_dirs inside the worker. See Testing Helpers.

Reporting a Bug

Include the output of python -m platformdirs, your operating system, how Python was installed (system, Homebrew, Microsoft Store, conda, ...) and the relevant environment variables. Issues go to the GitHub issue tracker.

See Also