1

xvfbwrapper requires python2.7 or up.

I have a script that does this:

from xvfbwrapper import Xvfb

I have python2.7 installed as an alternate installation of python because my OS (centOS 6.4) requires 2.6. When I run the script like this:

python2.7 some_script.py

I get this error:

ImportError: cannot import name Xvfb

This baffles me because the correct version of python is executing the script. It seems that the imported modules are trying to use the system version. If I change the python executable from /usr/local/bin/python2.7 to /usr/local/bin/python, I can open up the console and run the same import statement without getting the error if I ensure there are no byte code compiled files of the module from a previously failed execution, probably compiled with python2.6 for whatever reason.

It seemed like a path issues so I tried putting /usr/local/bin at the top of PATH and changing the name of python2.7 to python but no dice.

Any ideas about what is happening? I'm pulling my hair out trying to figure this out.

Thanks so much.

UPDATE:

xvfbwrapper was installed with:

pip-2.7 install xvfbwrapper

The same issue exists with virtualenv. *In the following example, some_script.py is simply an import statement:

""" some_script.py """
from xvfbwrapper import Xvfb

I ran the following commands:

$ virtualenv --no-site-packages --python=/usr/local/bin/python2.7 venv
$ source ./venv/bin/activate
$ pip install xvfbwrapper
$ python -v some_script.py

The partial output:

import xvfbwrapper # directory /home/projects/process/venv/lib/python2.7/site-packages/xvfbwrapper
# /home/projects/process/venv/lib/python2.7/site-packages/xvfbwrapper/__init__.pyc matches        /home/projects/process/venv/lib/python2.7/site-packages/xvfbwrapper/__init__.py
import xvfbwrapper # precompiled from /home/projects/process/venv/lib/python2.7/site-packages/xvfbwrapper/__init__.pyc
Traceback (most recent call last):
File "some_script.py", line 2, in <module>
    from xvfbwrapper import Xvfb
ImportError: cannot import name Xvfb

exact python version is 2.7.3 The xvfbwrapper module definitely has Xvfb class defined. I use the same module on a different system without any issues and it is the same version. (xvfbwrapper version 0.2.3)

To provide a little bit more context to the nature of the oddity: I can run the python console within the above virtual environment and successfully execute the same command.

UPDATE:

python2.7 -c "import sys; print sys.path"

The command outputs (*this is the system python, not the virtualenv):

['',
'/usr/local/lib/python2.7/site-packages/supervisor-3.0-py2.7.egg', 
'/usr/local/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg', 
'/usr/local/lib/python2.7/site-packages/virtualenv-1.10.1-py2.7.egg', 
'/usr/local/lib/python2.7/site-packages/distribute-0.7.3-py2.7.egg', 
'/usr/local/lib/python2.7/site-packages/setuptools-1.1.6-py2.7.egg', 
'/usr/local/lib/python27.zip', 
'/usr/local/lib/python2.7', 
'/usr/local/lib/python2.7/plat-linux2', 
'/usr/local/lib/python2.7/lib-tk', 
'/usr/local/lib/python2.7/lib-old', 
'/usr/local/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/site-packages']

within the virtualenv created:

['', 
'/home/projects/process/venv/lib/python27.zip', 
'/home/projects/process/venv/lib/python2.7', 
'/home/projects/process/venv/lib/python2.7/plat-linux2', 
'/home/projects/process/venv/lib/python2.7/lib-tk',     
'/home/projects/process/venv/lib/python2.7/lib-old', 
'/home/projects/process/venv/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7', 
'/usr/local/lib/python2.7/plat-linux2', 
'/usr/local/lib/python2.7/lib-tk', 
'/home/projects/process/venv/lib/python2.7/site-packages']
brando
  • 598
  • 6
  • 11

2 Answers2

1

Make sure you installed xvfbwrapper under Python 2.7; for that to work, there needs to be something like an easy_install-2.7 or pip-2.7 executable. If you just did sudo easy_install xvfbwrapper (or sudo pip install xvfbwrapper), only Python 2.6 would have that package available.

However, an better option is to use virtualenv (or Conda):

$ virtualenv --python=/usr/local/bin/python2.7 myproject
$ . myproject/bin/activate
$ pip install xvfbwrapper
$ python yourcode.py

See also: http://virtualenvwrapper.readthedocs.org/en/latest/

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • I used pip-2.7 to install xvfbwrapper. I was trying to avoid virtualenv because this is one of multiple similar scripts that need to be run by cron or triggered by other scripts that may be in different languages. Is there a way to use the virtualenv without writing a shell script to activate it? Thanks for your help so far. – brando Oct 05 '13 at 18:22
  • `path/to/venv/bin/python scriptname.py` – Erik Kaplun Oct 05 '13 at 20:35
  • I didn't realize it was that simple... that's good to know. I guess the activate script is just changing paths. However, that solution poses the same problem as described in the original post. I will update the main post with the addition of the virtualenv info. If you have any more ideas about what might be happening, I'd appreciate your thoughts. Thanks again. – brando Oct 06 '13 at 00:24
1

The xvfbwrapper version 0.2.3 contained a bug that was causing the problem.

(Sorry, when I checked the versions on my systems earlier I must've checked the same computer from the terminal on accident... I do that sometimes...)

I contacted the author and he graciously fixed the bug and released version 0.2.4 to PyPi which should work as expected.

The version on my other system that works was xvfbwrapper v0.2.2

brando
  • 598
  • 6
  • 11