0

My Ubuntu

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.4 LTS
Release:    14.04
Codename:   trusty

My Xvfb

$ apt --installed list | grep xvfb
xvfb/trusty-updates,trusty-security,now 2:1.15.1-0ubuntu2.7 amd64 [installed]

My python

python --version
Python 2.7.6

My file

$more pyvirtualdisplay.py

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

# now Firefox will run in a virtual display. 
# you will not see the browser.
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()

display.stop()

When I run it

$ python pyvirtualdisplay.py

Traceback (most recent call last):
  File "pyvirtualdisplay.py", line 3, in <module>
    from pyvirtualdisplay import Display
  File "/root/pyvirtualdisplay.py", line 3, in <module>
    from pyvirtualdisplay import Display
ImportError: cannot import name Display

PyVirtualDisplay

$ pip --version
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)

$ pip list | grep PyVirtual
PyVirtualDisplay (0.2)
zabumba
  • 12,172
  • 16
  • 72
  • 129

1 Answers1

2

The problem is you never define Display in your code. The name of your python code coincides with the module name called pyvirtualdisplay.

The file name of your python code is called pyvirtualdisplay.py and you also try to import from a module called pyvirtualdisplay.

As you can see from your pyvirtualdisplay.py, there is no function with name Display defined anywhere.

Python uses below search path in sequence to find the module you are trying to import:

  1. The home directory of the program

  2. Diretories in environment variable PYTHONPATH

  3. Standard library diretories

  4. The contents of any .pth files

So, in your case, python is trying to search in the same directory as you run your pyvirtualdisplay.py and it is able to find the file with name pyvirtualdisplay. So, python tries to search Display in pyvirtualdisplay.py (your own code) and thus, is not able to find anything related. That's why it is complaining cannot import name Display.

My suggestion is that you could change the name of your python code to a different one, e.g. pyvirtualdisplays.py and that will work.

zabumba
  • 12,172
  • 16
  • 72
  • 129
Michael Gao
  • 126
  • 5
  • oh god ... could that be trivial?! I feel awfully embarrassed. – zabumba Apr 16 '16 at 15:53
  • Yes of course!! Thanks for bothering answering such a silly problem, I am very very grateful. I was sidetracked by another issue. – zabumba Apr 16 '16 at 16:09