Python 通过 ssh 使用 tkinter 没有显示名称和 $DISPLAY 环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19309085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:24:44  来源:igfitidea点击:

No display name and no $DISPLAY environment variable using tkinter through ssh

pythonnumpysshmatplotlibtkinter

提问by Gabriel

I'm trying to run a very simple code that outputs a .png file in a cluster. Here's the code:

我正在尝试运行一个非常简单的代码,该代码在集群中输出一个 .png 文件。这是代码:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(60)
y = np.random.randn(60)

plt.scatter(x, y, s=20)

out_png = 'path/to/store/out_file.png'
plt.savefig(out_png, dpi=150)

If I run this code with the command python simple_code.pyin my system which has matplotlib 1.2.1 installed I get the warning:

如果我使用python simple_code.py安装了 matplotlib 1.2.1 的系统中的命令运行此代码,则会收到警告:

Unable to load library icui18n "Cannot load library icui18n:

The .png image is still produced so I have no problems here. But if I use the same command and code in a cluster which has matplotlib 1.3.0 installed it fails with the error:

.png 图像仍在生成,所以我在这里没有问题。但是如果我在安装了 matplotlib 1.3.0 的集群中使用相同的命令和代码,它会失败并显示错误:

Traceback (most recent call last):
  File "simple_code.py", line 33, in <module>
    plt.scatter(x, y, s=20)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 3078, in scatter
    ax = gca()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 803, in gca
    ax =  gcf().gca(**kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 450, in gcf
    return figure()
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1712, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

What is happening here?

这里发生了什么?



Add, this is the script I use to login into the cluster:

添加,这是我用来登录集群的脚本:

#!/usr/bin/expect

set login "user"
set addr "address1"
set addr2 "address2"
set pw "password"

spawn ssh -X $login@$addr
expect "$login@$addr\'s password:"
send "$pw\r"
expect "$login@host:"
send "ssh -X $addr2\r"
expect "$login@$addr\'s password:"
send "$pw\r"
interact

回答by luturo

Your problem is in ssh command. What you need to do is to write it this way:

您的问题出在 ssh 命令中。你需要做的是这样写:

ssh -X "your_login"