2

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.

plaes
  • 31,788
  • 11
  • 91
  • 89
Ozgur Akcali
  • 5,264
  • 2
  • 31
  • 49

2 Answers2

5

For future visitors, it is worth mentioning that PyVirtualDisplay offers an abstraction over Xvfb to make it easy to use from Python.

Oddthinking
  • 24,359
  • 19
  • 83
  • 121
4

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

Dan D.
  • 73,243
  • 15
  • 104
  • 123