[Python-Dev] Recommended way to tell platform

Tim Peters tim.peters at gmail.com
Sat Aug 7 20:35:25 CEST 2004


[Guido]
> This does bring up an interesting question: what's the recommended way
> to distinguish a certain platform?  There's os.name, sys.platform, the
> platform module, and I've also seen code testing for os.sep=='/'.

I ask myself that question every time I need to do it.  Out of sheer
momentum now, I almost always use

    if sys.platform in ('win32',):

I dislike using os.name == 'nt', mostly because if we asked all
Windows Python users which OS they use, only a tiny percentage would
answer "NT".  Since NT is a dying OS, 'nt' is an increasingly
odd-looking thing to test agaist.

OTOH, I have no idea when or why sys.platform == "win32" would give a
different result than os.name == "nt".

> I still prefer hasattr(<module>, <attribute>) whenever applicable,
> e.g. preferring hasattr(os, 'fork') over os.name=='posix' (or
> os.name!='nt' :-), but sometimes that's not possible.

Like whether to use time.time or time.clock.

> What should be the preferred way?  (It may be impossible to say
> because there are different use cases, but probably one of the most
> important cases is simply distinguishing Windows from the rest -- how
> should that be done?)

My way <wink> -- unless it's broken in some way I haven't bumped into yet.


More information about the Python-Dev mailing list