Detect 64bit OS (windows) in Python

Viewed 47724

Does anyone know how I would go about detected what bit version Windows is under Python. I need to know this as a way of using the right folder for Program Files.

Many thanks

22 Answers

I guess you should look in os.environ['PROGRAMFILES'] for the program files folder.

platform module -- Access to underlying platform’s identifying data

>>> import platform
>>> platform.architecture()
('32bit', 'WindowsPE')

On 64-bit Windows, 32-bit Python returns:

('32bit', 'WindowsPE')

And that means that this answer, even though it has been accepted, is incorrect. Please see some of the answers below for options that may work for different situations.

You should be using environment variables to access this. The program files directory is stored in the environment variable PROGRAMFILES on x86 Windows, the 32-bit program files is directory is stored in the PROGRAMFILES(X86) environment variable, these can be accessed by using os.environ('PROGRAMFILES').

Use sys.getwindowsversion() or the existence of PROGRAMFILES(X86) (if 'PROGRAMFILES(X86)' in os.environ) to determine what version of Windows you are using.

The subject lines asks about detecting 64 or 32bit OS, while the body talks about determining the location of ProgramFiles. The latter has a couple of workable answers here. I'd like to add another solution generalized to handle StartMenu, Desktop, etc. as well as ProgramFiles: How to get path of Start Menu's Programs directory?

64-bit versions of Windows use something called registry redirection and reflection keys. There is a compatibility layer called WoW64 which enables compatibility of 32-bit applications. Starting from Windows 7 and Windows Server 2008 R2 WoW64 registry keys are not longer reflected but shared. You can read about it here:

registry-reflection: msdn.microsoft.com/en-us/library/aa384235(v=vs.85).aspx

affected-keys: msdn.microsoft.com/en-us/library/aa384253(v=vs.85).aspx

wikipedia: en.wikipedia.org/wiki/WoW64

All you need to do is detect existence of those keys. You can use _winreg for that. Use try: and try opening key, example:

try:
aReg = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")

The solution posted by Alexander Brüsch is the correct solution, but it has a bug that only reveals itself on python3.x. He neglected to cast the returned value from GetCurrentProcess() to a HANDLE type. Passing a simple integer as the first parameter of IsWow64Process() returns 0 (which is an error flag from win32api). Also, Alexander incorrectly handles the return statement (success has no .value attribute).

For those who stumble on this thread, here is the corrected code:

import ctypes

def is64_bit_os():
    """Returns True if running 32-bit code on 64-bit operating system"""
    is64bit = ctypes.c_bool()
    handle = ctypes.wintypes.HANDLE(ctypes.windll.kernel32.GetCurrentProcess())
    success = ctypes.windll.kernel32.IsWow64Process(handle, ctypes.byref(is64bit))
    return success and is64bit.value
print(is64_bit_os())

There is a function named machine in platform module. I installed both Python3.8 32-bit and 64-bit versions on the same 64-bit machine with 64-bit Windows 10 and here is what I found:

Comparison of all the functions in platform module

And it looks like platform.machine returns machine architecture without bothering what type of python is installed. so here is my final compilation

import platform

def is_64bit():
    return platform.machine().endswith('64')

Most of the answers here are incorrect :/

Here is a simple translation of the well known method used in CMD and this is how microsoft do it too.

import os
_os_bit=64
if os.environ.get('PROCESSOR_ARCHITECTURE').lower() == 'x86' and os.environ.get('PROCESSOR_ARCHITEW6432') is None: _os_bit=32

print(_os_bit)

but remember: Windows 10 on ARM includes an x86-on-ARM64 emulation, so the possible values for PROCESSOR_ARCHITECTURE are: AMD64 or IA64 or ARM64 or x86

There should be a directory under Windows 64bit, a Folder called \Windows\WinSxS64 for 64 bit, under Windows 32bit, it's WinSxS.

Related