Skip to content

platformdirs ignores environment overrides like TMPDIR, XDG_*, and is therefore unreliable #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ooroor opened this issue Apr 7, 2025 · 1 comment

Comments

@ooroor
Copy link

ooroor commented Apr 7, 2025

Hi, and thanks for platformdirs.

After using the library in a cross-platform Python application, I've noticed that it ignores common environment variable overrides that define directory locations on most systems. This includes:

  • TMPDIR, TEMP, TMP on Unix/Linux/macOS
  • XDG_CONFIG_HOME, XDG_DATA_HOME, XDG_CACHE_HOME on Linux
  • Any user-specified overrides for Documents, Downloads, etc.

Example: Temp Directory

import os
from platformdirs import PlatformDirs

os.environ["TMPDIR"] = "/custom/tmp"
dirs = PlatformDirs("MyApp", "MyCompany")

print(dirs.user_cache_dir)  # Does NOT reflect TMPDIR override

Compare that with:

import tempfile
print(tempfile.gettempdir())  # Respects TMPDIR

Example: XDG Override Ignored

export XDG_CONFIG_HOME=/tmp/testconfig
python -c "from platformdirs import PlatformDirs; print(PlatformDirs('a', 'b').user_config_dir)"
# Still prints ~/.config/a

Expected Behavior

platformdirs should:

  • Use environment variable overrides as per platform conventions
  • Match the behavior of tempfile.gettempdir() (for temp dirs)
  • Match freedesktop spec on Linux (for XDG-based paths)

Current Result

The library hardcodes the path logic and doesn't reflect system/user overrides, making it unsuitable for any situation where the environment is customized — which is very common in packaging, CI/CD, containerized apps, multi-user systems, or advanced user environments.


Suggested Fix / Feature Request

  1. Fall back to current logic only if the environment variable is unset.
  2. Document clearly how the library deals with environment overrides.
  3. Optionally expose a temp_dir property that uses tempfile.gettempdir().

Conclusion

In its current state, platformdirs is nearly useless for real-world use cases where configuration directories, data paths, or temporary locations are customized by the environment. I'd strongly suggest making it respect the standard overrides, or at least make this behavior configurable.

Thanks for considering this!

@gaborbernat
Copy link
Member

In its current state, platformdirs is nearly useless for real-world use cases where configuration directories, data paths, or temporary locations are customized by the environment.

Please use more respectful tone.

path = os.environ.get("XDG_CONFIG_HOME", "")
shows that we do respect XDG_CONFIG_HOME, so can't replicate your issue. You might want to attach a reproducible with a Docker image, and more importantly submit PRs where you find feature gaps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants