@@ -530,15 +530,21 @@ def _get_config_or_cache_dir(xdg_base_getter):
530530 return str (configdir )
531531 # If the config or cache directory cannot be created or is not a writable
532532 # directory, create a temporary one.
533- tmpdir = os .environ ["MPLCONFIGDIR" ] = \
534- tempfile .mkdtemp (prefix = "matplotlib-" )
533+ try :
534+ tmpdir = tempfile .mkdtemp (prefix = "matplotlib-" )
535+ except OSError as exc :
536+ raise OSError (
537+ f"Matplotlib requires access to a writable cache directory, but the "
538+ f"default path ({ configdir } ) is not a writable directory, and a temporary "
539+ f"directory could not be created; set the MPLCONFIGDIR environment "
540+ f"variable to a writable directory" ) from exc
541+ os .environ ["MPLCONFIGDIR" ] = tmpdir
535542 atexit .register (shutil .rmtree , tmpdir )
536543 _log .warning (
537- "Matplotlib created a temporary config/cache directory at %s because "
538- "the default path (%s) is not a writable directory; it is highly "
539- "recommended to set the MPLCONFIGDIR environment variable to a "
540- "writable directory, in particular to speed up the import of "
541- "Matplotlib and to better support multiprocessing." ,
544+ "Matplotlib created a temporary cache directory at %s because the default path "
545+ "(%s) is not a writable directory; it is highly recommended to set the "
546+ "MPLCONFIGDIR environment variable to a writable directory, in particular to "
547+ "speed up the import of Matplotlib and to better support multiprocessing." ,
542548 tmpdir , configdir )
543549 return tmpdir
544550
0 commit comments