Detecting OS platform in Python

Discussion in 'Python' started by Devraj, Jan 11, 2008.

  1. Devraj

    Devraj Guest

    Hi everyone,

    My Python program needs reliably detect which Operating System its
    being run on, infact it even needs to know which distribution of say
    Linux its running on. The reason being its a GTK application that
    needs to adapt itself to be a Hildon application if run on devices
    like the N800.

    I have been searching around for an answer to this, and did find some
    messages on a lists that suggested the use of sys.platform to detect
    platform, with counter posts saying that it didn't work on Windows
    etc.

    Can anyone please shed some light on this?

    Thanks a lot.
    Devraj, Jan 11, 2008
    #1
    1. Advertising

  2. Devraj

    Mike Meyer Guest

    On Thu, 10 Jan 2008 18:37:59 -0800 (PST) Devraj <> wrote:

    > Hi everyone,
    >
    > My Python program needs reliably detect which Operating System its
    > being run on, infact it even needs to know which distribution of say
    > Linux its running on. The reason being its a GTK application that
    > needs to adapt itself to be a Hildon application if run on devices
    > like the N800.


    I don't think it can be done. For most Unix system, os.uname() will give
    you the information you want:

    >>> os.uname()

    ('FreeBSD', 'bhuda.mired.org', '6.2-STABLE', 'FreeBSD 6.2-STABLE #6: Sun Jun 3 04:17:59 EDT 2007 :/usr/src/sys/amd64/compile/BHUDA', 'amd64')

    (let's see - OS name, the system name, the release level, complete
    build information, down to the configuration file for the build and
    build number for that configuration, and the hardware type it was
    built for).

    For GNU/Linux systems, it won't, because it's a kernel facility, and
    kernels don't know what distribution they're part of:

    Linux student.mired.org 2.6.12-9-386 #1 Mon Oct 10 13:14:36 BST 2005 i686 GNU/Linux

    (kernel name, system name, kernel version and build information,
    platform, and operating system).

    GNU/Linux distributions are collections of lots of people software, so
    each has it's own way to state what "distribution" it is. I believe
    there's some sort of standard - except not everybody follows it. So
    you wind up using a series of heuristics to chase this information
    down.

    > I have been searching around for an answer to this, and did find some
    > messages on a lists that suggested the use of sys.platform to detect
    > platform, with counter posts saying that it didn't work on Windows
    > etc.


    Oh, you want it to work on Windows? Hmmm. [snotty comment about
    Windows deleted].

    Does the underlying platform claim to be POSIX.2 compliant? If so,
    then os.uname() should work on it, but as indicated, that may well not
    be complete.

    On the other hand, trying to figure out what features you have
    available by guessing based on the platform type is generally the
    wrong way to approach this kind of problem - only in part because you
    wind up reduced to a series of heuristics to figure out the
    platform. And once you've done that, you could wind up being wrong.

    Generally, you're better of probing the platform to find out if it has
    the facilities you're looking for. For python, that generally means
    trying to import the modules you need, and catching failures; or
    possibly looking for attributes on modules if they adopt to the
    environment around them.

    I'm not a GTK programmer, and have never even heard of Hildon. Is
    there some associated module you could try and import that doesn't
    exist on the N800? I.e.:

    try:
    import gtk
    mygui = 'gtk'
    except ImportError:
    import Hildon
    mygui = 'Hildon'

    or maybe something like:

    import gtk
    mygui = 'gtk' if not hasattr(gtk, 'Hildon') else 'Hildon'

    <mike
    --
    Mike Meyer <> http://www.mired.org/consulting.html
    Independent Network/Unix/Perforce consultant, email for more information.
    Mike Meyer, Jan 11, 2008
    #2
    1. Advertising

  3. Devraj

    Benjamin Guest

    On Jan 10, 8:37 pm, Devraj <> wrote:
    > Hi everyone,
    >
    > My Python program needs reliably detect which Operating System its
    > being run on, infact it even needs to know which distribution of say
    > Linux its running on. The reason being its a GTK application that
    > needs to adapt itself to be a Hildon application if run on devices
    > like the N800.

    platform.dist might help you. It's not very complete at all, though.
    (This is supposed to improve in 2.6, though)
    >
    > I have been searching around for an answer to this, and did find some
    > messages on a lists that suggested the use of sys.platform to detect
    > platform, with counter posts saying that it didn't work on Windows
    > etc.
    >
    > Can anyone please shed some light on this?
    >
    > Thanks a lot.
    Benjamin, Jan 11, 2008
    #3
  4. Devraj

    Paul Boddie Guest

    On 11 Jan, 04:14, Mike Meyer <>
    wrote:
    > On Thu, 10 Jan 2008 18:37:59 -0800 (PST) Devraj <> wrote:
    >
    > > My Python program needs reliably detect which Operating System its
    > > being run on, infact it even needs to know which distribution of say
    > > Linux its running on. The reason being its a GTK application that
    > > needs to adapt itself to be a Hildon application if run on devices
    > > like the N800.

    >
    > I don't think it can be done. For most Unix system, os.uname() will give
    > you the information you want:
    >
    > >>> os.uname()


    [...]

    > GNU/Linux distributions are collections of lots of people software, so
    > each has it's own way to state what "distribution" it is. I believe
    > there's some sort of standard - except not everybody follows it. So
    > you wind up using a series of heuristics to chase this information
    > down.


    On Red Hat distributions, there appears to be a file called /etc/
    redhat-release containing the distribution name. I can't remember the
    Debian equivalent, but it may be /etc/debian_version or something like
    that. There's LSB-related stuff, too, but I'm not sure if any of that
    tells you about the distribution itself.

    [...]

    > I'm not a GTK programmer, and have never even heard of Hildon. Is
    > there some associated module you could try and import that doesn't
    > exist on the N800? I.e.:
    >
    > try:
    > import gtk
    > mygui = 'gtk'
    > except ImportError:
    > import Hildon
    > mygui = 'Hildon'
    >
    > or maybe something like:
    >
    > import gtk
    > mygui = 'gtk' if not hasattr(gtk, 'Hildon') else 'Hildon'


    There must be Hildon-related modules or features that one can import
    or test for, as Mike suggests. In the desktop module [1], I generally
    test various environment variables or the output of various programs
    in order to deduce which desktop environment is being used, but this
    is in software which isn't generally trying to display a graphical
    user interface. Just testing for the presence of a file or a program
    isn't usually enough because on systems where desktop environments co-
    exist, for example, the presence of a GNOME-related file or program
    doesn't mean that the user is actually using GNOME at that particular
    time. If you might be using Hildon and are actually displaying a GUI,
    however, attempting to get the resources you need should be enough to
    confirm whether you're running Hildon or not.

    Paul

    [1] http://www.python.org/pypi/desktop
    Paul Boddie, Jan 11, 2008
    #4
  5. In article <>,
    Mike Meyer <> wrote:

    > On Thu, 10 Jan 2008 18:37:59 -0800 (PST) Devraj <> wrote:
    >
    > > Hi everyone,
    > >
    > > My Python program needs reliably detect which Operating System its
    > > being run on, infact it even needs to know which distribution of say
    > > Linux its running on. The reason being its a GTK application that
    > > needs to adapt itself to be a Hildon application if run on devices
    > > like the N800.

    >
    > I don't think it can be done.


    [...]

    > ...trying to figure out what features you have
    > available by guessing based on the platform type is generally the
    > wrong way to approach this kind of problem - only in part because you
    > wind up reduced to a series of heuristics to figure out the
    > platform. And once you've done that, you could wind up being wrong.
    >
    > Generally, you're better of probing the platform to find out if it has
    > the facilities you're looking for. For python, that generally means
    > trying to import the modules you need, and catching failures; or
    > possibly looking for attributes on modules if they adopt to the
    > environment around them.



    Much agreed. I just went through this with my SHM module. Compilation
    was failing because of a variation in ipc_perm in ipc.h on various
    platforms. I didn't feel confident at all that I could compile a list of
    all of the variations let alone keep it accurate and updated. The
    clincher was when I found that OS X >= 10.4 has two flavors of ipc_perm
    and which gets used depends on a compile flag, so identifying the OS
    would not have been useful in that case.

    OP, I don't know what a Hildon or N800 is, but is it possible that the
    same OS fingerprint could show up on different devices? If so then
    you're really out of luck. I think you'll be much better off if you
    focus less on the OS and more on the features it offers.

    Good luck

    --
    Philip
    http://NikitaTheSpider.com/
    Whole-site HTML validation, link checking and more
    Nikita the Spider, Jan 11, 2008
    #5
  6. Devraj

    MrJean1 Guest

    On Jan 10, 7:53 pm, Benjamin <> wrote:
    > On Jan 10, 8:37 pm, Devraj <> wrote:> Hi everyone,
    >
    > > My Python program needs reliably detect which Operating System its
    > > being run on, infact it even needs to know which distribution of say
    > > Linux its running on. The reason being its a GTK application that
    > > needs to adapt itself to be a Hildon application if run on devices
    > > like the N800.

    >
    > platform.dist might help you. It's not very complete at all, though.
    > (This is supposed to improve in 2.6, though)
    >


    Better yet, first use sys.platform. If that is 'linux2', then use
    platform.dist().

    /Jean Brouwers


    > > I have been searching around for an answer to this, and did find some
    > > messages on a lists that suggested the use of sys.platform to detect
    > > platform, with counter posts saying that it didn't work on Windows
    > > etc.

    >
    > > Can anyone please shed some light on this?

    >
    > > Thanks a lot.
    MrJean1, Jan 11, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jcc
    Replies:
    15
    Views:
    4,667
    Nigel Wade
    May 12, 2006
  2. Kevin Frey
    Replies:
    9
    Views:
    490
    Ron Natalie
    Aug 29, 2003
  3. Coca
    Replies:
    15
    Views:
    625
    Alan Balmer
    Jan 14, 2004
  4. Doug McCorkle
    Replies:
    0
    Views:
    230
    Doug McCorkle
    Jul 2, 2009
  5. Kaspar Schiess

    platform.rb (was: PLATFORM tests)

    Kaspar Schiess, Nov 25, 2005, in forum: Ruby
    Replies:
    0
    Views:
    109
    Kaspar Schiess
    Nov 25, 2005
Loading...

Share This Page