tags:

views:

239

answers:

4

how can i get from python that, what is my desktop environment i mean I like the result be gnome or KDE or else

+4  A: 

Tested in Ubuntu 9.10:

>>> import os
>>> os.environ.get('DESKTOP_SESSION')
'gnome'

Edit: As mentioned in comments below, this approach will not work for more some OSes. The other two answers provide workarounds.

Adam Bernier
and on fedora11! thanks a lot!
ali
Was just in the middle of writing an answer that involved enumerating processes but this is much better.
mdm
On Mac OS X 10.6.2 `os.environ.get('DESKTOP_SESSION')` returns `"None"`
Daniel Vassallo
i think because mac has only one!
ali
On my openSUSE 11.1 system, DESKTOP_SESSION is set to 'default'.
R Samuel Klatchko
This returns nothing at all for me. Rather, None.I'm using KDE on Ubuntu, but I'm not using a login manager (that is, no KDM or GDM).
Alex JL
+2  A: 

You might try this:

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

And read the discussion here: http://ubuntuforums.org/showthread.php?t=1139057

Mef
That works for my desktop to detect KDE (unlike DESKTOP_SESSION, which returns None)
Alex JL
+3  A: 

Sometimes people run a mix of desktop environments. Make your app desktop-agnostic using xdg-utils; that means using xdg-open to open a file or url, using xdg-user-dir DOCUMENTS to find the docs folder, xdg-email to send e-mail, and so on.

Tobu
A: 

sys.platform. Gritty details are in the documentation.

Bill Shob
`linux2` here; not a desktop environment.
Tobu