manage.py: cannot connect to X server
Asked Answered
J

3

4

I have used PyQt4.QtWebkit to crawl the web page in my django application.In the production environment that module doesn't work to crawl it.it throws the error "manage.py: cannot connect to X server"

My Qt class :

class Render(QWebPage):
 def __init__(self, url):
    self.app = QApplication(sys.argv)
    QWebPage.__init__(self)
    self.loadFinished.connect(self._loadFinished)
    self.mainFrame().load(QUrl(url))
    self.app.exec_()

 def _loadFinished(self, result):
    self.frame = self.mainFrame()
    self.app.quit() 

calling from django-shell:

 r = Render(url)

when i call this "Render" class through django with the Django-shell(python manage.py shell) the render function throws the error. could you please help me on this?

Johnnie answered 21/12, 2011 at 19:57 Comment(2)
What is the error? for me worksAcreinch
"manage.py: cannot connect to X server"Johnnie
J
9

The Reason is "Xvfb"

i need to run my python program in bash shell with xvfb(X virtual frame buffer) likewise,

ubuntu@localhost$ xvfb-run python webpage_scrapper.py http://www.google.ca/search?q=navaspot

It gives the result.

Now My requirement is i need to execute this shell command in python and waiting for tine to collect the result.I have to process the result.

Could you please suggest me for executing this command on python effectively.

Johnnie answered 22/12, 2011 at 15:44 Comment(1)
This works.Remember to remove the "os.putenv('DISPLAY', ':0.0')" from Marius' answer first though!Contentment
H
1

Seems like environment variables for X display are not set and that's the reason you get such error. It can occur because you're running script from environment, that isn't bound to X display (ssh to server).

Try adding display variable:

DISPLAY=:0.0 python manage.py script

It is also possible to set DISPLAY environment variable from python. You may set it before calling the PyQt4:

import os
os.putenv('DISPLAY', ':0.0')

It's also may not be possible to run PyQt4.QtWebkit if your production environment doesn't have X server running.

Hakan answered 21/12, 2011 at 23:5 Comment(2)
still I am getting "manage.py: cannot connect to X server :0.0" this error for this django command " DISPLAY=:0.0 python manage.py shell --settings=PROD_settings" ?Johnnie
Even I had tried in above both ways still the altered error is "manage.py: cannot connect to X server :0.0" but it works in my local machine.Johnnie
D
0

Generally on headless machines, the DISPLAY variable is absent or misconfigured. To work on such machines, you can use the following approach. As a example for Ubuntu 14.04-LTS machines:

First install X server: sudo apt-get install xserver-xorg

Now start the X server (say at :0): sudo /usr/bin/X :0&

You can use process managers like supervisor to handle the above process.

Now just set the DISPLAY environment variable and make sure it is available to any processes you are running which depend on this, DISPLAY=:0 python manage.py

The way you provide the environment variables to your application is upto you.

Dacca answered 26/12, 2015 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.