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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With