TclError:Google Colab 中没有显示名称且没有 $DISPLAY 环境变量

2024-02-05

这个错误:

TclError: no display name and no $DISPLAY environment variable

当我尝试在 Google Colab(协作 Jupyter 笔记本)中运行 Python 3.6 程序时,出现了这个问题。我在 Windows 10 计算机上的 Chrome 中运行 Colab。我在 Stack Overflow 上的其他线程中看到过此错误,但在 Colab 的上下文中却没有,并且之前发布的解决方案要么不适用,要么似乎不起作用。

我的代码是这样开始的:

from matplotlib.pyplot import *
from matplotlib.widgets import *
from math import *
from random import *
from numpy import *
from tkinter import *

请注意,为了使 tkinter 的导入起作用,我必须在不同的 Colab 单元中发出以下指令:

!apt-get install python3-tk

我从 Reddit 上的另一篇文章中发现,你可以创建一个虚拟显示器。下面的内容实际上适用于我的 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.

在我的具体案例中,我需要可视化 NLTK 树,因此我必须遵循以下附加步骤。

%matplotlib inline
### INSTALL GHOSTSCRIPT (Required to display NLTK trees) ###
!apt install ghostscript python3-tk
chunked_sentence = '(S (NP this tree) (VP (V is) (AdjP pretty)))'
from nltk.tree import Tree
from IPython.display import display
tree = Tree.fromstring(str(chunked_sentence))
display(tree)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TclError:Google Colab 中没有显示名称且没有 $DISPLAY 环境变量 的相关文章

随机推荐