Detecting OS platform in Python

Paul Boddie paul at boddie.org.uk
Fri Jan 11 08:38:38 EST 2008


On 11 Jan, 04:14, Mike Meyer <mwm-keyword-python.b4b... at mired.org>
wrote:
> On Thu, 10 Jan 2008 18:37:59 -0800 (PST) Devraj <dev... at gmail.com> 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



More information about the Python-list mailing list