Is it possible to use sys.platform == 'win32' to test on 64-bit Python?

The usual test for distinguishing between running a Python application on Windows and other operating systems (usually Linux) is to use a conditional:

if sys.platform == 'win32': ... 

But I wonder if it can be used today when 64-bit Python has been more widely used in recent years? Does 32 really mean 32-bit, or is it mainly related to the Win32 API?

If it is possible to have sys.platform one day as "win64", perhaps such a condition would be more universal?

 if sys.platform.startswith('win'): ... 

There is also another way to discover Windows that I know of:

 if os.name == 'nt': ... 

But I really never saw the use of the latter in other code.

What is the best way?

UPD . I would like to avoid using additional libraries if possible. The requirement to install an additional library to verify that I am not working on Windows can annoy Linux users.

+35
python windows cross-platform 64bit 32bit-64bit
Jan 27 '10 at 5:21
source share
4 answers

sys.platform will win32 regardless of the bitness of the base Windows system, as you can see in PC/pyconfig.h (from the original Python 2.6 source):

 #if defined(MS_WIN64) /* maintain "win32" sys.platform for backward compatibility of Python code, the Win64 API should be close enough to the Win32 API to make this preferable */ # define PLATFORM "win32" 

It is possible to find the original patch that introduced this on the Internet, which gives a little more explanation:

The main question is this: Win64 is much more similar to Win32 than different from it, that a regular Python programmer should not do differentiation in his Python code at all. Or, at least, it is enough that such differentiation using a Python script is rare enough, sufficient (even preferred). Currently, the answer is yes. I hope MS does not change this answer.

+29
Jan 27 '10 at 9:07
source share

Personally, I use platinfo to discover the underlying platform.

 >>> from platinfo import PlatInfo >>> pi = PlatInfo() >>> pi.os 'win64' >>> pi.arch 'x64' >>> pi.name() 'win64-x64' 

For the 32-bit version, pi.name() returns win32-x86 .

+3
Jan 27 '10 at 6:32
source share

Note that you cannot use sys.platform or os.name for this in Jython:

 $ jython -c "import sys, os; print sys.platform; print os.name" java1.6.0_20 java 

I think there is a project in the Jython project to change os.name to tell the underlying OS similar to CPython, but since people use os.name == 'java' to check if they are on Jython, this change cannot be made overnight. However, there is already os._name in Jython 2.5.x:

 $ jython -c "import os; print os._name" posix 

Personally, I tend to use os.sep == '/' with code that should run on both Jython and CPython, and on Windows and unixy platforms. This is somewhat ugly, but works.

+3
Sep 02 '10 at 10:37
source share

The reservations for Windows / 32 and Windows / 64 are the same, so they must use the same value. The only difference would be, for example, sys.maxint and ctypes . If you need to distinguish between 32 and 64, then platform is your best bet.

+1
Jan 27 '10 at 5:26
source share



All Articles