我有一个包含以下导入的 python 代码:
从 mpl_toolkits.mplot3d 导入 Axes3D
将 matplotlib.pyplot 导入为 plt
它在内部调用tkinter
。我正在使用装有 CentOS 的 Amazon EC2 服务器。我无法安装 tkinter,因为操作系统是 headless 的(没有 GUI)。 This link seems to be solution ,但它适用于 Debian 操作系统,不适用于 CentOS。
我尝试安装桌面引用 this link ,但这也行不通。它发出警告“警告:组 GNOME 桌面不存在”。当我运行命令 yum -y groups install "GNOME Desktop"
最佳答案
假设您根本不需要 GUI,但让 matplotlib 在您的服务器上生成图像,以下内容可能会有所帮助:
使用没有交互元素的后端根本不需要 tkinter。
来自documentation :
There are two types of backends: user interface backends [...] and hardcopy backends to make image files (PNG, SVG, PDF, PS; also referred to as “non-interactive backends”).
设置后端的两种方法(也取自上面的链接):
matplotlibrc 文件中的后端参数(参见 Customizing matplotlib ):
backend : Agg
脚本内部
import matplotlib matplotlib.use('Agg')
如果使用
use()
函数,则必须在导入 matplotlib.pyplot 之前完成。
可能的非交互式后端:
关于python - amazon ec2 centOS 上没有名为 tkinter 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45126445/