Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我当前的桌面环境是什么?_Python_Linux_Environment - Fatal编程技术网

Python 我当前的桌面环境是什么?

Python 我当前的桌面环境是什么?,python,linux,environment,Python,Linux,Environment,我怎样才能知道我的桌面环境在使用Python?我希望结果是gnome或KDE或其他。在Ubuntu 9.10中测试: >>> import os >>> os.environ.get('DESKTOP_SESSION') 'gnome' 编辑:正如下面评论中提到的,这种方法对更多的操作系统不起作用。其他两个答案提供了解决办法。您可以尝试以下方法: def detect_desktop_environment(): desktop_environmen

我怎样才能知道我的桌面环境在使用Python?我希望结果是gnome或KDE或其他。

在Ubuntu 9.10中测试:

>>> import os
>>> os.environ.get('DESKTOP_SESSION')
'gnome'
编辑:正如下面评论中提到的,这种方法对更多的操作系统不起作用。其他两个答案提供了解决办法。

您可以尝试以下方法:

def detect_desktop_environment():
    desktop_environment = 'generic'
    if os.environ.get('KDE_FULL_SESSION') == 'true':
        desktop_environment = 'kde'
    elif os.environ.get('GNOME_DESKTOP_SESSION_ID'):
        desktop_environment = 'gnome'
    else:
        try:
            info = getoutput('xprop -root _DT_SAVE_MODE')
            if ' = "xfce4"' in info:
                desktop_environment = 'xfce'
        except (OSError, RuntimeError):
            pass
    return desktop_environment

阅读这里的讨论:

有时人们会混合使用桌面环境。使用xdg utils使应用程序桌面不可知;这意味着使用
xdg open
打开文件或url,使用
xdg user dir DOCUMENTS
查找文档文件夹,使用
xdg email
发送电子邮件,等等。

我在我的一个项目中使用此选项:

    def get_desktop_environment(self):
        #From http://stackoverflow.com/questions/2035657/what-is-my-current-desktop-environment
        # and http://ubuntuforums.org/showthread.php?t=652320
        # and http://ubuntuforums.org/showthread.php?t=652320
        # and http://ubuntuforums.org/showthread.php?t=1139057
        if sys.platform in ["win32", "cygwin"]:
            return "windows"
        elif sys.platform == "darwin":
            return "mac"
        else: #Most likely either a POSIX system or something not much common
            desktop_session = os.environ.get("DESKTOP_SESSION")
            if desktop_session is not None: #easier to match if we doesn't have  to deal with caracter cases
                desktop_session = desktop_session.lower()
                if desktop_session in ["gnome","unity", "cinnamon", "mate", "xfce4", "lxde", "fluxbox", 
                                       "blackbox", "openbox", "icewm", "jwm", "afterstep","trinity", "kde"]:
                    return desktop_session
                ## Special cases ##
                # Canonical sets $DESKTOP_SESSION to Lubuntu rather than LXDE if using LXDE.
                # There is no guarantee that they will not do the same with the other desktop environments.
                elif "xfce" in desktop_session or desktop_session.startswith("xubuntu"):
                    return "xfce4"
                elif desktop_session.startswith("ubuntu"):
                    return "unity"       
                elif desktop_session.startswith("lubuntu"):
                    return "lxde" 
                elif desktop_session.startswith("kubuntu"): 
                    return "kde" 
                elif desktop_session.startswith("razor"): # e.g. razorkwin
                    return "razor-qt"
                elif desktop_session.startswith("wmaker"): # e.g. wmaker-common
                    return "windowmaker"
            if os.environ.get('KDE_FULL_SESSION') == 'true':
                return "kde"
            elif os.environ.get('GNOME_DESKTOP_SESSION_ID'):
                if not "deprecated" in os.environ.get('GNOME_DESKTOP_SESSION_ID'):
                    return "gnome2"
            #From http://ubuntuforums.org/showthread.php?t=652320
            elif self.is_running("xfce-mcs-manage"):
                return "xfce4"
            elif self.is_running("ksmserver"):
                return "kde"
        return "unknown"

    def is_running(self, process):
        #From http://www.bloggerpolis.com/2011/05/how-to-check-if-a-process-is-running-using-python/
        # and http://richarddingwall.name/2009/06/18/windows-equivalents-of-ps-and-kill-commands/
        try: #Linux/Unix
            s = subprocess.Popen(["ps", "axw"],stdout=subprocess.PIPE)
        except: #Windows
            s = subprocess.Popen(["tasklist", "/v"],stdout=subprocess.PIPE)
        for x in s.stdout:
            if re.search(process, x):
                return True
        return False

正在编写一个涉及枚举进程的答案,但这是更好的。在Mac OS x0.62<代码> OS.Envial.GET(“DealTopSub会话”)< /C> >返回<代码>“没有”< /代码>我认为因为Mac只有一个!在我的openSUSE 11.1系统上,桌面会话设置为“默认”。为我返回“ubuntu”。在Ubuntu 12.04上使用stock Unity。使用此方法的任何人都必须进行一些测试以找到各种响应,例如,如果您试图确定是否需要GTK/QT用户界面,则这并不好。这对我的桌面检测KDE(不同于桌面_会话,它不返回任何内容)的更新非常有效。使用
xprop-root | grep-io“xfce”
,然后至少在“xfce”和“lxde”之间交替使用。这适用于Raspbian和Ubuntu Studio。将输出更改为小写,以便更好地理解或选择语句。谢谢!这太完美了!你应该为它制作一个Python模块并把它放在PyPI上。我应该在Ubuntu Studio上添加这个模块,
os.environ.get(“桌面会话”)
抛出“ubuntududio”。要获得正确的桌面环境,我使用
os.environ['XDG\u CURRENT\u Desktop'].lower()
获得'xfce'。这是一个扩展这段很棒代码的变通方法。上传它这在LinuxMint17.3中不起作用,一直返回未知