Running Xvfb from Python
-
25-10-2019 - |
문제
I am working on a server with no X servers and trying to run a script that uses spynner module, which requires an X server. For this purpose, I want to run Xvfb.
I can run the script by calling it via xvfb-run
, i.e.:
xvfb-run python2.6 try.py.
This works with no problem. However, I need to invoke Xvfb from within the script. For this purpose, I tried using subprocess as follows:
xvfb = subprocess.Popen(['Xvfb', ':99'])
After adding this piece of code to the beginning of the script, and trying to run the script as
python2.6 try.py
I get the message:
: cannot connect to X server
Is there something else I need to do? Thanks in advance.
해결책
you'll need to add:
import os
os.environ["DISPLAY"]=":99"
so that when it goes to open the connection to the X server it'll be able to find the Xvfb instance you've started
다른 팁
For future visitors, it is worth mentioning that PyVirtualDisplay offers an abstraction over Xvfb to make it easy to use from Python.