Skip to content

Added handling for undetermined home directory #30454

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

Merged
merged 2 commits into from
Aug 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,37 +532,48 @@ def _get_config_or_cache_dir(xdg_base_getter):
elif sys.platform.startswith(('linux', 'freebsd')):
# Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first,
# as _xdg_base_getter can throw.
configdir = Path(xdg_base_getter(), "matplotlib")
try:
configdir = Path(xdg_base_getter(), "matplotlib")
except RuntimeError: # raised if Path.home() is not available
pass
else:
configdir = Path.home() / ".matplotlib"
# Resolve the path to handle potential issues with inaccessible symlinks.
configdir = configdir.resolve()
try:
configdir.mkdir(parents=True, exist_ok=True)
except OSError as exc:
_log.warning("mkdir -p failed for path %s: %s", configdir, exc)
try:
configdir = Path.home() / ".matplotlib"
except RuntimeError: # raised if Path.home() is not available
pass

if configdir:
# Resolve the path to handle potential issues with inaccessible symlinks.
configdir = configdir.resolve()
try:
configdir.mkdir(parents=True, exist_ok=True)
except OSError as exc:
_log.warning("mkdir -p failed for path %s: %s", configdir, exc)
else:
if os.access(str(configdir), os.W_OK) and configdir.is_dir():
return str(configdir)
_log.warning("%s is not a writable directory", configdir)
issue_msg = "the default path ({configdir})"
else:
if os.access(str(configdir), os.W_OK) and configdir.is_dir():
return str(configdir)
_log.warning("%s is not a writable directory", configdir)
issue_msg = "resolving the home directory"
# If the config or cache directory cannot be created or is not a writable
# directory, create a temporary one.
try:
tmpdir = tempfile.mkdtemp(prefix="matplotlib-")
except OSError as exc:
raise OSError(
f"Matplotlib requires access to a writable cache directory, but there "
f"was an issue with the default path ({configdir}), and a temporary "
f"was an issue with {issue_msg}, and a temporary "
f"directory could not be created; set the MPLCONFIGDIR environment "
f"variable to a writable directory") from exc
os.environ["MPLCONFIGDIR"] = tmpdir
atexit.register(shutil.rmtree, tmpdir)
_log.warning(
"Matplotlib created a temporary cache directory at %s because there was "
"an issue with the default path (%s); it is highly recommended to set the "
"an issue with %s; it is highly recommended to set the "
"MPLCONFIGDIR environment variable to a writable directory, in particular to "
"speed up the import of Matplotlib and to better support multiprocessing.",
tmpdir, configdir)
tmpdir, issue_msg)
return tmpdir


Expand Down
Loading