Launch terminal via Python in different linux distros

Launch terminal via Python in different linux distros

I’ve recently switched to KDE Neon for different experience, and realize that my python scripts for executing commands via launching terminal stopped working. And it hits me that specifying “gnome-terminal” would limit the scripts to only work on distros that comes with “gnome-terminal”, and KDE Neon doesn’t have it.

As I looked into if there’s a command that simply launches the default terminal program, I realize there may not be one. My journey takes me to a github code here, which loops through a list of terminal commands and launches one that’s available. Referencing that and using methods listed here, I wrote a python function below that loops through the list of terminal commands, and return the first executable command.

from distutils.spawn import find_executable

def isNot_exec(name):
    #check whether terminal program exists
    return find_executable(name) is None

def findTerminal():
    termList = ["x-terminal-emulator", "konsole", "gnome-terminal", "urxvt", "rxvt", "termit", "terminator", "Eterm", "aterm", "uxterm", "xterm", "roxterm", "xfce4-terminal", "termite", "lxterminal", "mate-terminal", "terminology", "st", "qterminal", "lilyterm", "tilix", "terminix", "kitty", "guake", "tilda", "alacritty", "hyper"]
    #list taken from
    i = 0
    for term in termList:
        if isNot_exec(term):
            i += 1
    return termList[i]

Note that instead of using shutil.which, I’m using distutils.spawn.find_executable, which does not actually verify that the command is executable, merely verify that it is in PATH. That is because my intended use of this script is for The Foundry’s Nuke, and it’s still using python 2.7.3, which does not have shutil.which

One thought on “Launch terminal via Python in different linux distros

Leave a Reply

Your email address will not be published. Required fields are marked *