473,388 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 4516
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stefan Behrens | last post by:
Hi all, does anyone know a way to detect whether the user is "active" at the desktop? What I mean is, whether he/she is actively using the machine by typing or moving the mouse. What I am...
3
by: Alessandro Crugnola *sephiroth* | last post by:
Hi, i'm using wxPython and wxTextStyled. How can I intercept document changes for active documents (for example modified with others external editors)? thanks in advance -- Alessandro...
4
by: Daniel Orner | last post by:
Does anyone know of a simple way to have a Python script find out what browser is accessing it? After a web search the only thing I found to do this is Zope, but the system I'm programming doesn't...
11
by: Woojay Jeon | last post by:
OK, I tried a Google search on this Usenet group but couldn't find a solution, so I'm posting my question here (if there's a better archive than the one in Google, please let me know). Does...
15
by: Jay | last post by:
I'm sure this is a really dumb question, but how do you detect a variable type in Python? For example, I want to know if the variable "a" is a list of strings or a single string. How do I do...
7
by: BWill | last post by:
Hi, I'm writing a file browser, but I'm not sure how I could go about detecting the drives available on windows and linux systems (preferably using the standard modules if possible). I guess I...
8
by: dwelch91 | last post by:
I need to detect whether the operating system I am running on (not the Python version) is 64bit or 32bit. One requirement is that I need to include support for non-Intel/AMD architectures. The 2...
16
by: bobrik | last post by:
Hello, I am using the Python DB API for access to MySQL. But it is not platform-independent - I need a module not included in Python by default - python-mysql, and it uses a compiled binary...
3
by: saneman | last post by:
I have read that Python is a platform independent language. But on this page: http://docs.python.org/tut/node4.html#SECTION004220000000000000000 it seems that making a python script...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.