设置Matplotlib MPLCONFIGDIR:考虑将MPLCONFIGDIR设置为matplotlib配置数据的可写目录 [英] Setting Matplotlib MPLCONFIGDIR: consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data

查看:672
本文介绍了设置Matplotlib MPLCONFIGDIR:考虑将MPLCONFIGDIR设置为matplotlib配置数据的可写目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Linux服务器来设置django项目.我收到此错误:无法创建/var/www/.matplotlib;请考虑将MPLCONFIGDIR设置为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"

然后我发现$ MPLCONFIGDIR为空.因此,我将其设置如下:

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

此路径是我要存储Matplotlib创建的图像的目录. 然后,请确保在python命令行中使用此设置:

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'

但是,在使用mod_wsgi在Apache中部署的django项目中,上述错误仍然存​​在.我添加了以下几行:

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'))

它打印无"!

有人可以帮助我吗?

谢谢.

推荐答案

在导入matplotlib之前,在代码中设置MPLCONFIGDIR.确保目录具有权限,以便应用程序可以将其写入.

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

这篇关于设置Matplotlib MPLCONFIGDIR:考虑将MPLCONFIGDIR设置为matplotlib配置数据的可写目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆