XDG Base Directories
The `xdg-base-dirs` library provides Python functions to retrieve paths adhering to the XDG Base Directory Specification, which defines standard locations for user-specific data, configuration, and cache files. As of version 6.0.2, it is actively maintained with frequent minor updates and fixes, and a major release approximately once a year.
Warnings
- breaking The library was renamed from `xdg` to `xdg-base-dirs` in v6.0.0 due to an import collision with another package. This change requires updating both your dependency name (e.g., in `requirements.txt`) and your import statements.
- breaking Python 3.10 or later is now required for `xdg-base-dirs`.
- breaking The API refactored in v5.0.0 to provide functions rather than module-level variables. While the old variable-based API is maintained for backward compatibility, it is no longer documented and may be removed in future major versions. Functions ensure that environment variable changes made after import are respected.
- breaking As of v4.0.0, paths are returned as `pathlib.Path` objects instead of strings. This is generally preferred but requires conversion if your code expects string paths.
- gotcha Relative paths defined in XDG environment variables (e.g., `XDG_CONFIG_HOME`) are considered invalid by the specification and are ignored by the library. Only absolute paths are accepted.
Install
-
pip install xdg-base-dirs
Imports
- xdg_config_home
from xdg_base_dirs import xdg_config_home
Quickstart
from xdg_base_dirs import xdg_config_home, xdg_data_home, xdg_cache_home, xdg_runtime_dir
# Get the path to the user's XDG config directory
config_dir = xdg_config_home()
print(f"XDG Config Home: {config_dir}")
# Get the path to the user's XDG data directory
data_dir = xdg_data_home()
print(f"XDG Data Home: {data_dir}")
# Paths are returned as pathlib.Path objects
assert isinstance(config_dir, type(xdg_config_home()))