Installing Matplotlib MPLCONFIGDIR: Consider installing MPLCONFIGDIR in a writable directory for matplotlib configuration data

I am using a Linux server to create a django project. I got this error: "Could not create /var/www/.matplotlib; consider installing MPLCONFIGDIR in a writable directory for matplotlib configuration data"

Then I found that $ MPLCONFIGDIR is empty. So I installed it like this:

lab@base:~$ export MPLCONFIGDIR=~/website/graph lab@base:~$ echo $MPLCONFIGDIR /home/lab/website/graph 

This path is the directory in which I want to store the images created by Matplotlib. Then I made sure that this parameter on the python command line:

 >>> import matplotlib >>> import os >>> os.environ.get('MPLCONFIGDIR') '/home/lab/website/graph' 

BUT, in the django project, which is deployed in Apache with mod_wsgi, the above error still comes out. I added the following lines:

 import os os.environ['MPLCONFIGDIR'] = "/home/lab/website/graph" print(os.environ.get('MPLCONFIGDIR')) 

He prints "No"!

Can anybody help me?

Thanks.

+8
matplotlib
source share
2 answers

Install MPLCONFIGDIR in the code before importing matplotlib. Make sure the directory has permissions so that the application can be written.

 import os os.environ['MPLCONFIGDIR'] = "/home/lab/website/graph" import matplotlib 

Alternatively, you can install it in a temp file.

 import os import tempfile os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp() import matplotlib 
+18
source share

Per @Esteban I do something similar in modules or scripts:

 import os try: import matplotlib except: import tempfile import atexit import shutil mpldir = tempfile.mkdtemp() atexit.register(shutil.rmtree, mpldir) # rm directory on succ exit os.environ['MPLCONFIGDIR'] = mpldir import matplotlib 

Therefore, the temporary directory is deleted upon exit.

0
source share

All Articles