开发者

python gtk module opens display on import

开发者 https://www.devze.com 2022-12-13 08:30 出处:网络
I\'m using the trick \"python -c \'import myscript.py\'\" to perform a syntax check on a script which uses \'import gtk\'.

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 perform开发者_StackOverflow社区ing 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.


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.


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).


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


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'"


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

0

精彩评论

暂无评论...
验证码 换一张
取 消