Fix Matplotlib MPLCONFIGDIR error
When using Apache + python-cgi, I encounter this error:
<type ‘exceptions.RuntimeError’>: Failed to create /$dirstring$/common/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data
An official explanation of MPLCONFIGDIR is here.
MPLCONFIGDIR This is the directory used to store user customizations to matplotlib, as well as some caches to improve performance. If MPLCONFIGDIR is not defined, HOME/.matplotlib is used by default.
It turns out that on my windows machine HOME/.matplotlib is now writable. Here’s a way to simply bypass this problem.
Find out the place where you first import matplotlib, right before it, set the $HOME dir to a temporary dir where it’s writable:
For example, I was importing matplotlib in an indirect way through “from scipy.cluster.vq import kmeans2“, so right before it, do
os.environ[‘HOME’] = r’C:\……\Apache2.2\htdocs\tmp’
from scipy.cluster.vq import kmeans2