Migrating from appdirs

platformdirs was introduced in 2021 as a friendly fork of appdirs. It keeps the appdirs API, so most projects migrate by changing one import. This page lists what stays the same, what differs, and what you gain.

The Short Version

Replace the dependency in your package metadata:

[project]
dependencies = [
    "platformdirs",
]

and the imports:

# Before
from appdirs import user_data_dir, AppDirs

# After
from platformdirs import user_data_dir, AppDirs

If you prefer the smallest possible diff, alias the module:

import platformdirs as appdirs

appdirs.user_cache_dir("SuperApp", "Acme")

What Is Compatible

The platformdirs test suite (tests/test_comp_with_appdirs.py) installs appdirs and checks that:

  • every public function of appdirs exists in platformdirs;
  • every property of appdirs.AppDirs exists on platformdirs.AppDirs;
  • platformdirs.AppDirs is the same object as platformdirs.PlatformDirs;
  • with the XDG variables cleared, these functions return the same paths as appdirs on the test platform: site_config_dir, site_data_dir, user_cache_dir, user_config_dir, user_data_dir, user_state_dir, and on Windows also user_log_dir.

The positional parameter order of the classic functions (appname, appauthor, version, roaming / multipath / opinion) is unchanged, so positional calls keep working.

What Differs

Log Directory on Linux

user_log_dir("SuperApp") on Linux
appdirs ~/.cache/SuperApp/log
platformdirs ~/.local/state/SuperApp/log

platformdirs follows the XDG specification, which defines XDG_STATE_HOME for logs and history. If you have existing log files, look in the old location once, or simply let old logs expire.

Log Directory on macOS Without an App Name

appdirs.user_log_dir() fails on macOS when no app name is given; platformdirs returns ~/Library/Logs.

Homebrew Python on macOS

When Python comes from Homebrew, platformdirs returns Homebrew locations for some site directories (<prefix>/share/SuperApp, <prefix>/var/cache/SuperApp) where appdirs returns /Library/.... See macOS.

Stricter Names

platformdirs rejects an appname, appauthor or version that would escape the base directory (absolute paths, drives, .. components) with ValueError. appdirs joined them blindly. See Parameters.

Python Version

platformdirs 4.14.9 requires Python 3.11 or newer. If you must support older interpreters, pin an older platformdirs release that supports them.

What You Gain

Feature Where
pathlib.Path variants of every function and property (user_data_path, ...) Module Functions
ensure_exists to create directories, with safe POSIX permissions Directory Creation and Safety
New directories: cache/state/log per site, runtime, preference, media folders (documents, downloads, pictures, videos, music, desktop, projects, public share, templates), fonts, bin and applications Choosing a Directory
Search-path iterators and find_*_file / place_*_file helpers The PlatformDirs Class
Android and iOS support Android, iOS
use_site_for_root for system services Parameters
WIN_PD_OVERRIDE_* variables on Windows Environment Variables
Test isolation helpers and a pytest fixture Testing Helpers
Type hints (py.typed) the whole package

Checklist

  1. Swap the dependency and imports.
  2. Run python -m platformdirs on each platform you support and compare with what your app used before (see Command Line Report).
  3. If you write logs on Linux, decide whether to migrate old files from ~/.cache/<app>/log.
  4. Consider switching to the *_path functions and ensure_exists=True to remove your own os.makedirs calls.

See Also