Detecting OS platform in Python

Mike Meyer mwm-keyword-python.b4bdba at mired.org
Thu Jan 10 22:14:31 EST 2008


On Thu, 10 Jan 2008 18:37:59 -0800 (PST) Devraj <devraj at gmail.com> 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     mwm at bhuda.mired.org:/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 <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.



More information about the Python-list mailing list