Skip to content

Commit 43ae77e

Browse files
committed
Create a config dir in tempfile.gettempdir() if the default location(s) are not writable.
1 parent 42878d2 commit 43ae77e

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lib/matplotlib/__init__.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,18 @@ def _get_home():
466466
raise RuntimeError('please define environment variable $HOME')
467467

468468

469+
def _create_tmp_config_dir():
470+
"""
471+
If the config directory can not be created, create a temporary
472+
directory that is destroyed atexit.
473+
"""
474+
import tempfile
475+
476+
tempdir = os.path.join(tempfile.gettempdir(), 'matplotlib')
477+
os.environ['MPLCONFIGDIR'] = tempdir
478+
479+
return tempdir
480+
469481

470482
get_home = verbose.wrap('$HOME=%s', _get_home, always=False)
471483

@@ -482,18 +494,18 @@ def _get_configdir():
482494
if not os.path.exists(configdir):
483495
os.makedirs(configdir)
484496
if not _is_writable_dir(configdir):
485-
raise RuntimeError('Could not write to MPLCONFIGDIR="%s"'%configdir)
497+
return _create_tmp_config_dir()
486498
return configdir
487499

488500
h = get_home()
489501
p = os.path.join(get_home(), '.matplotlib')
490502

491503
if os.path.exists(p):
492504
if not _is_writable_dir(p):
493-
raise RuntimeError("'%s' is not a writable dir; you must set %s/.matplotlib to be a writable dir. You can also set environment variable MPLCONFIGDIR to any writable directory where you want matplotlib data stored "% (h, h))
505+
return _create_tmp_config_dir()
494506
else:
495507
if not _is_writable_dir(h):
496-
raise RuntimeError("Failed to create %s/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"%h)
508+
return _create_tmp_config_dir()
497509
from matplotlib.cbook import mkdirs
498510
mkdirs(p)
499511

@@ -922,7 +934,7 @@ class rc_context(object):
922934
>>> with mpl.rc_context(fname='print.rc'):
923935
>>> plt.plot(x, b)
924936
>>> plt.plot(x, c)
925-
937+
926938
The 'a' vs 'x' and 'c' vs 'x' plots would have settings from
927939
'screen.rc', while the 'b' vs 'x' plot would have settings from
928940
'print.rc'.

0 commit comments

Comments
 (0)