tags:

views:

234

answers:

5

I'm using the trick "python -c 'import myscript.py'" to perform a syntax check on a script which uses 'import gtk'.

I get the following error when performing the syntax check, which implies that the gtk module is executing a check for the X display, even though all that's being done at this point is to import the module.

Traceback (most recent call last):

  File "<stdin>", line 15, in ?

      File "myscript.py", line 21, in ?

    import gtk

  File "/usr/src/build/463937-i386/install/usr/lib/python2.3/site-packages/gtk-2.0/gtk/__init__.py", line 37, in ?

RuntimeError: could not open display

Is there a way to avoid this error when performing the syntax check?

Before you ask - I'm not able to set $DISPLAY before the syntax check is run. The check is being run on remote servers as part of a distributed build system. These servers do not have an X display available.

A: 

What exactly do you mean by 'syntax checking'?
Can't you use a tool like pylint to check for syntax errors?

Otherwise: a very ugly (but probably possible hack):

  1. In your python script detect whether X is present.
  2. If it's not => use GTK on DirectFramebuffer (no X needed then). You'll need to compile GTK on DirectFB (and/or pygtk) from source (some pointers here).
ChristopheD
A: 

If the remote machine has vncserver installed, you can have a dummy server running and connect to that. Sample instructions:

remotemachine $ vncserver -depth 16 -geometry 800x600 :7
New 'X' desktop is remotemachine:7

Starting applications specified in /home/user/.vnc/xstartup
Log file is /home/user/.vnc/userve:7.log
remotemachine $ DISPLAY=:7 python -c 'import myscript.py'
…
remotemachine $ vncserver -kill :7
Killing Xtightvnc process ID 32058
ΤΖΩΤΖΙΟΥ
The X.Org distribution ships `Xvfb` and `Xfake`, which can be used for similar purposes. Also, I'd probably use `startx \`which python\` -c myscript.py -- \`which Xvnc\` :7`, which will automatically set up the environment for the client and teardown the X server once it's done.
ephemient
@ephemient: Based on the question information, I assumed that there isn't any X.Org installation in the remote servers.
ΤΖΩΤΖΙΟΥ
Right, but the UNIX VNC server contains port of X.Org anyhow. If OP needs X.Org parts, it doesn't matter which parts they are as long as they work; there's nothing particularly special about `Xvnc`.
ephemient
I don't know why you need to say the obvious: "there's nothing special about Xvnc" and whether there is shared code between Xvnc and X.Org. I'm trying to re-read my texts to see if, perhaps, I come through as believing that Xvnc is a panacea for all server and administrator issues. Instead, I think that between X.Org and Xvnc an administrator would *obviously* select Xvnc just for its smaller package size, *assuming both are not installed*, and that is why I can't understand what's the issue you're insisting on.
ΤΖΩΤΖΙΟΥ
The Xvnc sources contain a copy of the X.Org xserver; Xfake and Xvfb are built from a subset of the X.Org xserver. Either way, there is no need to install the entire X.Org distribution, and `apt-get install [vncserver/xvfb/...]` or whatever is appropriate for OP's system will probably pull down the minimal bits required. If OP has PyGTK, all the required X client library bits are installed anyways, so a lot of the X.Org distribution is already present.
ephemient
OK, thanks. Hopefully one can install a small subset of X.Org+Xfake/Xvfb that is size-wise comparable to Xvnc. I won't update my answer, though, since this isn't something that I personally know/tested.
ΤΖΩΤΖΙΟΥ
A: 

In your myscript.py, you could do like this

if __name__=="__main__":
    import gtk

That will not execute gtk's __init__.py when you do "python -c 'import myscript.py'"

S.Mark
But if the script accesses anything in `gtk.` outside of functions (e.g. inherits from a gtk class), it will fail with a NameError.
Beni Cherniavsky-Paskin
A: 

Importing modules in Python executes their code!
Well-behaved modules use the if __name__ == '__main__' trick to avoid side effects, but they can still fail - as happened to you.

[BTW, getting to ImportError means the whole file already has correct syntax.]

If you just want to check syntax, without running at all:

  • python -m py_compile my_script.py
    will check one file (and produce a .pyc as a side effect).

  • python -m compileall ./
    will check a whole dir recursively.

  • python -c 'compile(open("myscript.py").read(), "myscript.py", "exec")'
    avoids creating a .pyc.

But note that merely checking the syntax in Python catches very few bugs! Importing does catch more, e.g. mispelled names. For even better checks, use tools like Pychecker / Pyflakes.

Beni Cherniavsky-Paskin
A: 

If you are editing with IDLE, Alt+X will check syntax of current file without running it.

Beni Cherniavsky-Paskin