_tkinter.TclError : 표시 이름 없음 및 $ DISPLAY 환경 변수 없음
서버에서 간단한 파이썬 스크립트를 실행 중입니다.
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)
python example.py
matplotlib 1.5.1이 설치된이 서버 에서 명령을 사용하려고 하면 오류와 함께 실패합니다.
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
여기서 무슨 일이 일어나고 있습니까?
Matplotlib은 기본적으로 Xwindows 백엔드를 선택합니다. Xwindows 백엔드를 사용하지 않으려면 matplotlib를 설정해야합니다.
이 코드를 스크립트 시작 부분에 추가하고 ( 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
당신은이 두 줄을 추가하여 해결할 수있는 아주 당신의 평 스크립트의 시작.
import matplotlib
matplotlib.use('Agg')
추신 : 소스 코드의 맨 처음 에이 두 줄을 추가하지 않으면 오류가 계속 발생합니다.
답을 더하기 위해 필요한 스크립트의 시작 부분에서 이것을 사용했습니다. 따라서 다양한 환경에서 원활하게 실행됩니다.
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를 통과 할 때만 백엔드를 사용하고 싶지 않기 때문에 .
I had this same issue trying to run a simple tkinter app remotely on a Raspberry Pi. In my case I did want to display the tkinter GUI on the pi display, but I want to be able to execute it over SSH from my host machine. I was also not using matplotlib, so that wasn't the cause of my issue. I was able to resolve the issue by setting the DISPLAY environment variable as the error suggests with the command:
export DISPLAY=:0.0
A good explanation of what the display environment variable is doing and why the syntax is so odd can be found here: https://askubuntu.com/questions/432255/what-is-display-environment-variable
I also met this problem while using Xshell to connect Linux server.
After seaching for methods, I find Xming + Xshell to solve image imshow problem with matplotlib.
If solutions aboved can't solve your problem, just try to download Xming under the condition you're using Xshell. Then set the attribute in Xshell, SSH->tunnel->X11transfer->choose X DISPLAY localhost:0.0
Another solution is to install Xvfb, and export your display to it. ie:
disp=:8
screen=0
geom=640x480x24
exec Xvfb $disp -screen $screen $geom 2>/tmp/Xvfb.log &
Then
$ export DISPLAY=:8
$ ./example.py
'IT' 카테고리의 다른 글
Git 병합 롤백 (0) | 2020.06.04 |
---|---|
필터링 애플리케이션을위한 elasticsearch vs MongoDB (0) | 2020.06.04 |
서클의 마우스 오버에 대한 데이터 표시 (0) | 2020.06.04 |
오버플로 : 끝에 숨겨진 점 (0) | 2020.06.04 |
MySQL을 사용하도록 장고 설정 (0) | 2020.06.04 |