The usual check to differentiate between running Python-application on Windows and on other OSes (Linux typically) is to use conditional:
if sys.platform == 'win32':
...
But I wonder is it safe to use today when 64-bit Python is more widely used in last years? Does 32 really means 32-bit, or basically it refers to Win32 API?
If there is possibility to have one day sys.platform as 'win64' maybe such condition would be more universal?
if sys.platform.startswith('win'):
...
There is also another way to detect Windows I'm aware of:
if os.name == 'nt':
...
But I really never saw in other code the use of the latter.
What is the best way then?
UPD: I'd like to avoid using extra libraries if I can. Requiring installing extra library to check that I'm work not in the Windows may be annoying for Linux users.
Notice that you cannot use either
sys.platform
oros.name
for this on Jython:I think there's a plan in Jython project to change
os.name
to report the underlying OS similarly as CPython, but because people are usingos.name == 'java'
to check are they on Jython this change cannot be done overnight. There is, however, alreadyos._name
on Jython 2.5.x:Personally I tend to use
os.sep == '/'
with code that needs to run both on Jython and CPython, and both on Windows and unixy platforms. It's somewhat ugly but works.Personally I use platinfo for detecting the underlying platform.
For 32-bit,
pi.name()
returnswin32-x86
.sys.platform
will bewin32
regardless of the bitness of the underlying Windows system, as you can see inPC/pyconfig.h
(from the Python 2.6 source distribution):It's possible to find the original patch that introduced this on the web, which offers a bit more explanation:
The caveats for Windows/32 and Windows/64 are the same, so they should use the same value. The only difference would be in e.g.
sys.maxint
andctypes
. If you need to distinguish between 32 and 64 regardless thenplatform
is your best bet.