Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 352
bialix Avatar asked Jan 27 '10 05:01

bialix


People also ask

Does Win32 work on 64bit?

(2) Win32 is the programming interface (API) for 32-bit and 64-bit Windows operating systems. Starting with Windows 95, developers write applications that call the routines in the Win32 API.

What does Sys platform do?

sys. platform lets you inspect what operating system/platform the code is running on. This can be very useful, for example, in projects which interface with non-Python aspects of the operating system.

How do I know if my python is Windows OS?

system() Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. An empty string is returned if the value cannot be determined. If that isn't working, maybe try platform.


1 Answers

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

#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's possible to find the original patch that introduced this on the web, which offers a bit more explanation:

The main question is: is Win64 so much more like Win32 than different from it that the common-case general Python programmer should not ever have to make the differentiation in his Python code. Or, at least, enough so that such differentiation by the Python scriptor is rare enough that some other provided mechanism is sufficient (even preferable). Currently the answer is yes. Hopefully MS will not change this answer.

like image 55
Torsten Marek Avatar answered Sep 18 '22 13:09

Torsten Marek