471,349 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Detecting OS platform in Python

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.
Jan 11 '08 #1
4 4404
On Thu, 10 Jan 2008 18:37:59 -0800 (PST) Devraj <de****@gmail.comwrote:
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 mw*@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 <mw*@mired.org> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
Jan 11 '08 #2
On Jan 10, 8:37 pm, Devraj <dev...@gmail.comwrote:
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.
Jan 11 '08 #3
In article <ma************************************@python.org >,
Mike Meyer <mw***********************@mired.orgwrote:
On Thu, 10 Jan 2008 18:37:59 -0800 (PST) Devraj <de****@gmail.comwrote:
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
Jan 11 '08 #4
On Jan 10, 7:53*pm, Benjamin <musiccomposit...@gmail.comwrote:
On Jan 10, 8:37 pm, Devraj <dev...@gmail.comwrote: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.
Jan 11 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stefan Behrens | last post: by
3 posts views Thread by Alessandro Crugnola *sephiroth* | last post: by
4 posts views Thread by Daniel Orner | last post: by
11 posts views Thread by Woojay Jeon | last post: by
15 posts views Thread by Jay | last post: by
7 posts views Thread by BWill | last post: by
8 posts views Thread by dwelch91 | last post: by
3 posts views Thread by saneman | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.