Skip to content

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

Open
@ooroor

Description

@ooroor

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions