_tkinter.TclError:没有显示名称,也没有 $DISPLAY 环境变量
- 2025-01-22 08:46:00
- admin 原创
- 96
问题描述:
我在服务器中运行一个简单的 python 脚本:
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)
我尝试在安装了 matplotlib 1.5.1 的服务器中使用该命令python example.py
,但失败并出现错误:
Traceback (most recent call last):
File "example.py", line 7, in <module>
plt.scatter(x, y, s=20)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3241, in scatter
ax = gca()
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 928, in gca
return gcf().gca(**kwargs)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 578, in gcf
return figure()
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 527, in figure
**kwargs)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure
window = Tk.Tk()
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1810, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
这里发生了什么事?
解决方案 1:
Matplotlib 默认选择 Xwindows 后端。您需要将 matplotlib 设置为不使用 Xwindows 后端。
将此代码添加到脚本的开头(在导入 pyplot 之前)并重试:
import matplotlib
matplotlib.use('Agg')
或者添加到.config/matplotlib/matplotlibrc
行backend: Agg
以使用非交互式后端。
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc
或者当连接到服务器时使用ssh -X remoteMachine
命令来使用Xwindows。
您也可以尝试导出显示:export DISPLAY=mymachine.com:0.0
。
更多信息:https ://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
解决方案 2:
您可以在.py 脚本的最开始处添加这两行来解决此问题。
import matplotlib
matplotlib.use('Agg')
PS:如果源代码最开始没有加上这两行,错误依然存在。
解决方案 3:
补充一下答案,我在所需脚本的开头使用了它。因此它可以在不同的环境中顺利运行。
import os
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
print('no display found. Using non-interactive Agg backend')
mpl.use('Agg')
import matplotlib.pyplot as plt
因为我不想让它总是使用'Agg'
后端,只有当它通过 Travis CI 时才使用。
解决方案 4:
我在尝试在 Raspberry Pi 上远程运行一个简单的 tkinter 应用程序时遇到了同样的问题。就我而言,我确实想在 pi 显示屏上显示 tkinter GUI,但我希望能够通过 SSH 从我的主机执行它。我也没有使用 matplotlib,所以这不是我的问题的原因。我能够通过设置 DISPLAY 环境变量来解决问题,正如错误所建议的那样,使用以下命令:
export DISPLAY=:0.0
关于显示环境变量的作用以及其语法如此奇怪的原因,可以在这里找到很好的解释:https://askubuntu.com/questions/432255/what-is-display-environment-variable
解决方案 5:
我在 Colab 上遇到了这个问题,下面几行代码解决了它。把它放在笔记本的开头。
参考
### CREATE VIRTUAL DISPLAY ###
!apt-get install -y xvfb # Install X Virtual Frame Buffer
import os
os.system('Xvfb :1 -screen 0 1600x1200x16 &') # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8
os.environ['DISPLAY']=':1.0' # tell X clients to use our virtual DISPLAY :1.0.
%matplotlib inline
### INSTALL GHOSTSCRIPT (Required to display NLTK trees) ###
!apt install ghostscript python3-tk
解决方案 6:
另一个解决方案是安装 Xvfb,并将您的显示内容导出到其中。即:
disp=:8
screen=0
geom=640x480x24
exec Xvfb $disp -screen $screen $geom 2>/tmp/Xvfb.log &
然后
$导出显示=:8
$./example.py
解决方案 7:
为了查看远程机器上的窗口上显示的图像、图表和任何内容,您需要像这样连接到它:
ssh -X user@hostname
这样,您便可以访问 X 服务器。X 服务器是 X Window 系统中运行在本地计算机(即用户直接使用的计算机)上的程序,负责处理对这些计算机上的显卡、显示屏和输入设备(通常是键盘和鼠标)的所有访问。
更多信息请见此处。
解决方案 8:
我在使用Xshell连接Linux服务器的时候也遇到了这个问题。
经过寻找方法,我发现Xming + Xshell可以解决matplotlib的图像显示问题。
如果以上方法都不能解决你的问题,那么就尝试在Xshell下下载Xming,然后在Xshell中设置属性,SSH->tunnel->X11transfer->choose X DISPLAY localhost:0.0
解决方案 9:
我想在这里添加一个答案,没有人明确说明实施方案。
这是解决此故障的绝佳参考资源:
https://matplotlib.org/faq/usage_faq.html
在我的例子中,使用matplotlib.use
不起作用,因为它不知何故已经在其他地方设置了。不过,我可以通过定义环境变量来克服错误:
export MPLBACKEND=Agg
这样就解决了这个问题。
我的错误具体出现在 CircleCI 流程中,这解决了失败的测试。奇怪的是,我的测试在使用运行时会通过,但与功能一起pytest
使用时会失败。不过,声明此环境变量解决了这个问题。parallelism
`circleci tests split`
解决方案 10:
尽管有解决方案use('Agg')
,但有人可能对解决 x11 感兴趣。
假设我们有一个用户ml
,在 ubuntu20 上使用 python3......
首先,以以下身份登录ml
,然后写入ml.py
$ cat ml.py
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 = 'out_file.png'
plt.savefig(out_png, dpi=150)
在运行文件之前,我们应该设置x11,
$ whoami
ml
$ touch ~/.Xauthority
$ ls .Xauthority
.Xauthority
# only this one key is needed for X11 over SSH
xauth generate :14 . trusted
$ echo $DISPLAY
localhost:14.0
$ xauth list
ml/unix:14 MIT-MAGIC-COOKIE-1 b6a2833785df5e75e2c8bc34045a55a6
跑步...
$ python3 ml.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-rk288ipf because the default path (/home/ml/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
Matplotlib is building the font cache; this may take a moment.
qt.qpa.xcb: X server does not support XInput 2
failed to get the current screen resources
好吧,更新 OpenGL 和 x11-apps,重新运行
$ python3 ml.py
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-87at6bw8 because the default path (/home/zzx/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
qt.qpa.xcb: X server does not support XInput 2
failed to get the current screen resources
似乎我们无法得到out_file.png
,但是......
$ export QT_DEBUG_PLUGINS=1
重新运行文件,我们得到图像,:)