6

Firstly, what is the best/simplest way to detect if X11 is running and available for a python script.

parent process?
session leader?
X environment variables?
other?

Secondly, I would like to have a utility (python script) to present a gui if available, otherwise use a command line backed tool.

Off the top of my head I thought of this

-main python script (detects if gui is available and launches appropriate script)
-gui or command line python script starts
-both use a generic module to do actual work

I am very open to suggestions to simplify this.

3
  • 1
    By "X" do you mean "The X Windowing System"? Or do you mean "X" as in "Some Random Piece of Software"?
    – S.Lott
    Jun 22, 2009 at 15:42
  • The GUI part of the question tends to disambiguate 'X', but I would agree specifying X11 might make a clearer question.
    – Erik
    Jun 22, 2009 at 15:52
  • @Erik: To me GUI meant "console app for random piece of software X". To prevent duplicate questions, it helps to get each question reasonably clear.
    – S.Lott
    Jun 22, 2009 at 15:57

3 Answers 3

14

Check the return code of xset -q:

def X_is_running():
    from subprocess import Popen, PIPE
    p = Popen(["xset", "-q"], stdout=PIPE, stderr=PIPE)
    p.communicate()
    return p.returncode == 0

As for the second part of your question, I suggest the following main.py structure:

import common_lib

def gui_main():
    ...

def cli_main():
    ...

def X_is_running():
    ...

if __name__ == '__main__':
    if X_is_running():
        gui_main()
    else:
        cli_main()
1
  • Thank you. The approach of checking the xset-return code was more helful to me than just checking the DISPLAY variable: In my case, the DISPLAY vairable was set, but X11 connections did not work due to some unrelated network/firewall issues.
    – dasmy
    Nov 17, 2020 at 8:31
10

I'd check to see if DISPLAY is set ( this is what C API X11 applications do after all ).

import os

if os.environ.get('DISPLAY'):
    print("X11 is available")
5

You could simply launch the gui part, and catch the exception it raises when X (or any other platform dependent graphics system is not available.

Make sure you really have an interactive terminal before running the text based part. Your process might have been started without a visible terminal, as is common in graphical user environments like KDE, gnome or windows.

3
  • Indeed, this would be my favored approach as well. It's common, too: last I checked, Redhat's Anaconda installer does this too.
    – ephemient
    Jun 23, 2009 at 3:01
  • This was the easiest. I think the DISPLAY answer is good, but the python exceptions will have already happened; depending on where you wanted to try to import. ie import gtk causes an exception if your DISPLAY wasn't set (with my experience). I suppose if you carried enough you could check DISPLAY then do imports for gui libraries. One more plus for DISPLAY is, I feel, based on purest programing practices where exceptions should not handle program logic (if you want to class it as checking DISPLAY as program logic)
    – user125959
    Jun 23, 2009 at 13:51
  • What exception would be raised?
    – norok2
    Jul 6, 2019 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.