GtkWarning:无法打开显示

2024-04-25 06:19:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个vps上运行一个spider(使用scrapyjs,它使用python-gtk2)。在运行蜘蛛时,我得到了错误

/root/myporj/venv/local/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display

如何在无头安装中运行此程序?


Tags: gtkvenvinitvpslibpackageslocaldist
2条回答

首先,您没有指定服务器上是否安装了桌面环境(或X)?

不管怎样,您可以使用xvfb来实现蜘蛛的无头设置:

Xvfb or X virtual framebuffer is a display server implementing the X11 display server protocol. In contrast to other display servers Xvfb performs all graphical operations in memory without showing any screen output. From the point of view of the client, it acts exactly like any other X display server, serving requests and sending events and errors as appropriate. However, no output is shown. This virtual server does not require the computer it is running on to even have a screen or any input device.

首先,在服务器上安装xvfb。假设您正在使用apt-get

sudo apt-get install xvfb

现在,在一个自定义的显示号码上运行xvfb服务,比如:

sudo Xvfb :5

之后,通过导出显示号并运行应用程序,您可以在虚拟帧缓冲区中运行任何需要X的应用程序:

export DISPLAY=:5
run_my_application

DISPLAY只为当前终端会话设置,因此在实现此步骤的自动化时请记住这一点。

可能您是通过ssh运行应用程序的?然后,您需要在本地PC上运行X服务器。如果不是,则应正确设置DISPLAY环境变量。最近,Ubuntu上的sudo命令没有传输环境变量。如果在sudo上运行应用程序,请确保$DISPLAY是否已设置。

附加

是个侏儒虫。你的应用程序应该依赖于gtk小部件。

https://bugs.launchpad.net/ubuntu/+source/software-center/+bug/800574

相关问题 更多 >