Results 1 to 8 of 8

Thread: Python check desktop environment

  1. #1
    Join Date
    Feb 2009
    Location
    Netherlands
    Beans
    784

    Python check desktop environment

    Hi all. I'm having a problem with a python script that is supposed to find out the users desktop environment and act accordingly. To keep it simple I reduced it to this:
    Code:
    #!/usr/bin/env python
    import sys, os
    
    desktoptype = os.environ.get('DESKTOP_SESSION')
    
    if 'gnome' in desktoptype:
       print 'Gnome!'
    if 'kde' in desktoptype:
       print 'KDE!'
    else:
       print 'Failed!'
    It should (in my case) print 'Gnome'! but it doesn't. Any ideas on this? Thanks in advance. Btw, I have python 2.6 and 3.0 on Jaunty.

  2. #2
    Join Date
    Sep 2007
    Beans
    Hidden!
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python check desktop environment

    I believe DESKTOP_SESSION only gets set if a person is running gdm, but I guess you are. ( I'm not and it isn't set for me )

    Other than that I think that DESKTOP_SESSION can also be set to default, which your code doesn't catch.

  3. #3
    Join Date
    Apr 2006
    Location
    Hamilton, New Zealand
    Beans
    198
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python check desktop environment

    Have you manually checked what the result is from running

    Code:
    os.environ.get('DESKTOP_SESSION')
    I just tested it on ubuntu 8.10 and it returned 'default', I assume that it will do the same when running it inside KDE on Kubuntu. So you will also have to take into account what distro you are running.

  4. #4
    Join Date
    Sep 2007
    Beans
    Hidden!
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python check desktop environment

    As I said, I believe that is only set by gdm, so people running Kubuntu probably won't have it set ( they use kdm instead ).

    Personally I launch straight into my desktop without using gdm/kdm/etc so I won't have it set either.

    Not sure if there is a cleaner way of detecting this info, perhaps checking the process list who be more accurate?

  5. #5
    Join Date
    Feb 2009
    Location
    Netherlands
    Beans
    784

    Re: Python check desktop environment

    Yes, default it is. Thanks, I should have thought of checking the output first. Not sure how to do things now, but this is solved. Thanks for the quick replies.
    Another question: where does it get the 'default' from? Why does it not return gnome or compiz or whatever?
    Ah, I see the remark about only gdm setting this. Well, back to the drawing table.
    Last edited by VCoolio; April 27th, 2009 at 12:54 AM. Reason: Read first, post later

  6. #6
    Join Date
    Jun 2007
    Beans
    692

    Re: Python check desktop environment

    I came across this a while ago
    Code:
    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
    Back when I was looking for a way to do it I couldn't find anything out there that was completely reliable, but this has worked pretty well.

  7. #7
    Join Date
    Aug 2008
    Beans
    6

    Re: Python check desktop environment

    GNOME_DESKTOP_SESSION_ID is deprecated in recent versions.
    Anyone knows a good alternative?

    I came up with something, but i don't know how reliable it is.

    Code:
    from subprocess import Popen, PIPE
    proc = Popen(["pidof", "gnome-session"], stdout=PIPE, stderr=PIPE)
    if proc.communicate()[0]:
      desktop_environment = 'gnome'

  8. #8
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    978
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python check desktop environment

    I think you may be hitting a dead end here. There is no environment variable that lists exactly what DE/WM you are using. The best you could do is query the process list and use a case statement with the 10 most common ones.

    For example, you don't currently take into consideration that I might be running something other than KDE or Gnome (with or without compiz) - I could be running IceWM or Fluxbox

    I cant see anything from the LSB on this - http://refspecs.freestandards.org/LS...ric/book1.html
    Last edited by Bodsda; September 6th, 2011 at 09:32 AM.
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •