python - amazon ec2 centOS 上没有名为 tkinter 的模块

标签 python linux matplotlib amazon-ec2 centos

我有一个包含以下导入的 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”).

设置后端的两种方法(也取自上面的链接):

  1. matplotlibrc 文件中的后端参数(参见 Customizing matplotlib ):

    backend : Agg  
    
  2. 脚本内部

    import matplotlib
    matplotlib.use('Agg')  
    

    如果使用 use() 函数,则必须在导入 matplotlib.pyplot 之前完成。

可能的非交互式后端:

enter image description here

关于python - amazon ec2 centOS 上没有名为 tkinter 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45126445/

相关文章:

python - 使用 pytest 时出现 “indirect fixture” 错误。怎么了?

c++ - 为什么在linux上用久了物理内存一直在增加,虚拟内存大小会增加一点

linux - 如何在gedit上重做

python - 如何使获取方法返回 INTEGER 列的 int 值而不是字符串 Python sqlite?

python - MatLab 与 Python - 向量化和 reshape 数组

linux - 使用 VPN 连接仅路由特定 IP 范围

python - 如何绘制 sympy 微分与数组和循环方法?

python - 在 networkx 中绘制完整的 neo4j 图形的最简单方法

python - 如何根据其他列 'Applicant_Income' 的条件绘制列 'Education"的直方图?

python - 在scrapy中提取em标签内的文本