Detect 64bit OS (windows) in Python

williamtroup picture williamtroup · Feb 5, 2010 · Viewed 40.9k times · Source

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

Answer

phobie picture phobie · Sep 25, 2012

I think the best solution to the problem has been posted by Mark Ribau.

The best answer to the question for Python 2.7 and newer is:

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

On windows the cross-platform-function platform.machine() internally uses the environmental variables used in Matthew Scoutens answer.

I found the following values:

  • WinXP-32: x86
  • Vista-32: x86
  • Win7-64: AMD64
  • Debian-32: i686
  • Debian-64: x86_64

For Python 2.6 and older:

def is_windows_64bit():
    if 'PROCESSOR_ARCHITEW6432' in os.environ:
        return True
    return os.environ['PROCESSOR_ARCHITECTURE'].endswith('64')

To find the Python interpreter bit version I use:

def is_python_64bit():
    return (struct.calcsize("P") == 8)