Detect if X11 is available (python)

First, what is the best / easiest way to determine if X11 works and if a python script is available.

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

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

From the top of my head I thought about it

-main python script (determines if gui is available and runs the corresponding script)
-gui or command line python script runs
- use a common module to do the actual work.

I am very open to suggestions to simplify this.

+4
source share
3 answers

You can simply run the gui part and catch the exception that it throws when X (or any other platform-dependent graphics system is unavailable.

Before starting the text part, make sure that you really have an interactive terminal. Your process may have started without a visible terminal, as is usually the case in graphical user environments such as KDE, gnome, or windows.

+4
source

I would see if DISPLAY is installed (these are still C API X11 applications).

+9
source

Check the return code 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() 
+7
source

Source: https://habr.com/ru/post/1286578/


All Articles