Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Matplotlib MPLCONFIGDIR: consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data

Tags:

matplotlib

I am using Linux server to set up a django project. I got this error: "Failed to create /var/www/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"

Then I found $MPLCONFIGDIR are empty. So I set it like this:

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

This path is the directory where I want to store images created by Matplotlib. Then I made sure this setting in 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-mentioned error still exits. I added the below lines:

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

It prints "None"!

Can anyone help me?

Thanks.

like image 392
Munichong Avatar asked Mar 22 '12 17:03

Munichong


1 Answers

Set the MPLCONFIGDIR in code before you import matplotlib. Make sure the directory has permissions such that it can be written to by the app.

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

Alternatively, you could set it to a tempfile.

import os    
import tempfile
os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
import matplotlib
like image 118
Esteban Avatar answered Oct 25 '22 09:10

Esteban