Detect 64bit OS (windows) in Python

46,441

Solution 1

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.

Solution 2

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)

Solution 3

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

Solution 4

Came here searching for properly detecting if running on 64bit windows, compiling all the above into something more concise.

Below you will find a function to test if running on 64bit windows, a function to get the 32bit Program Files folder, and a function to get the 64bit Program Files folder; all regardless of running 32bit or 64bit python. When running 32bit python, most things report as if 32bit when running on 64bit, even os.environ['PROGRAMFILES'].

import os

def Is64Windows():
    return 'PROGRAMFILES(X86)' in os.environ

def GetProgramFiles32():
    if Is64Windows():
        return os.environ['PROGRAMFILES(X86)']
    else:
        return os.environ['PROGRAMFILES']

def GetProgramFiles64():
    if Is64Windows():
        return os.environ['PROGRAMW6432']
    else:
        return None

Note: Yes, this is a bit hackish. All other methods that "should just work", do not work when running 32bit Python on 64bit Windows (at least for the various 2.x and 3.x versions I have tried).

Edits:
2011-09-07 - Added a note about why only this hackish method works properly.

Solution 5

def os_platform():
    true_platform = os.environ['PROCESSOR_ARCHITECTURE']
    try:
            true_platform = os.environ["PROCESSOR_ARCHITEW6432"]
    except KeyError:
            pass
            #true_platform not assigned to if this does not exist
    return true_platform

http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx

Share:
46,441
William Troup
Author by

William Troup

Software Engineer/Architect, writer, designer, etc

Updated on July 09, 2022

Comments

  • William Troup
    William Troup almost 2 years

    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

    • Ignacio Vazquez-Abrams
      Ignacio Vazquez-Abrams about 14 years
      Knowing this won't tell you where the program files are stored.
    • whunmr
      whunmr about 14 years
      >>> import ctypes, sys >>> i = ctypes.c_int() >>> kernel32 = ctypes.windll.kernel32 >>> process = kernel32.GetCurrentProcess() >>> kernel32.IsWow64Process(process, ctypes.byref(i)) gossamer-threads.com/lists/python/python/663523
    • Petri
      Petri over 7 years
      When reading through the answers, be warned: some of them return the version (32/64bit) of installed Python, some the version of the processor architecture, and only some actually return the version (32/64bit) of the OS. Which is what the question asks for. Many of the answers confuse these.
  • BlueRaja - Danny Pflughoeft
    BlueRaja - Danny Pflughoeft about 14 years
    +1 for solving the problem instead of answering the question - not always a good thing, but in this case it is.
  • Joe Koberg
    Joe Koberg about 14 years
    But I agree, use the environment variable to locate %PROGRAMFILES%
  • Mike Graham
    Mike Graham about 14 years
    This is the right solution, rather than hardcoding a directory. However, this will not lead to the 32-bit program files on 64-bit Windows, if that's what is needed.
  • Mike Graham
    Mike Graham about 14 years
    This is fragile and hackish. Python provides built-in ways to directly access information about the OS, and it doesn't depend on the Windows installation directory being in a normal place or having a normal name.
  • Meh
    Meh about 14 years
    On 64-bit Windows it returns the same thing.
  • Meh
    Meh about 14 years
    Yes, it will lead to the correct Program Files on Win64. Here are my values on Win7 64: ProgramFiles=C:\Program Files (x86) ProgramFiles(x86)=C:\Program Files (x86) ProgramW6432=C:\Program Files
  • Thomas Ahle
    Thomas Ahle about 14 years
    This won't work, because python chose to always return win32 for compatibility. This is also why there are only 'hackish' way to find out.
  • Tadeck
    Tadeck almost 13 years
    @Thomas If I do print platform.architecture()[0], it displays 64bit on Windows 7 64bit.
  • Mark Ribau
    Mark Ribau almost 13 years
    @Tadeck You are probably running 64 bit Python on 64 bit Windows.
  • Tadeck
    Tadeck almost 13 years
    @Mark Ribau: Maybe you are right, but when I start IDLE, it shows me "Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32".
  • Mark Ribau
    Mark Ribau over 12 years
    @Mike Except that it doesn't work on Windows most of the time. Try running a 32bit Python on 64bit windows. It returns 32bit for most cases if using a default compile from python.org. (At least for all the 2.x versions I have used.)
  • Mark Ribau
    Mark Ribau over 12 years
    @Tadeck That looks like 2.7.1 64bit. Running platform.architecture() in six different versions of IDLE on Windows 7 64bit yields: Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32 (2.6.6 32bit) => ('32bit', 'WindowsPE'); Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] on win32 (2.6.6 64bit) => ('64bit', 'WindowsPE'); Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 (2.7.2 32bit) => ('32bit', 'WindowsPE');
  • Mark Ribau
    Mark Ribau over 12 years
    Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32 (2.7.2 64bit) => ('64bit', 'WindowsPE'); Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 (3.2.2 32bit) => ('32bit', 'WindowsPE'); Python 3.2.2 (default, Sep 4 2011, 09:07:29) [MSC v.1500 64 bit (AMD64)] on win32 (3.2.2 64bit) => ('64bit', 'WindowsPE').
  • Mark Ribau
    Mark Ribau over 12 years
    this does indeed return the true platform, even on 32bit python running on 64bit windows; however, true_platform will be "x86" for 32bit python on 32bit windows, "AMD64" for 32bit or 64bit python on windows 64bit for many machines (most intel consumer CPU, most AMD consumer CPU), "I64" for 32bit or 64bit python on windows 64bit for itanium and similar machines. just be aware of this.
  • Mark Ribau
    Mark Ribau about 12 years
    FYI: It will lead to the x86 version if you are running 32 bit python, but the x64 version if running 64 bit python.
  • phobie
    phobie over 11 years
    getwindowsversion() does not lead to the bit version used by the os! I. e. it returns similar results for Win7 32-bit and 64-bit. For the environmental variables have a look at the answer of Mark Ribau!
  • max
    max over 9 years
    No. I ran this on a 64 bit windows server 2012R2, it returned False but worked on windows 7. Better use reg keys or environment variables.
  • Dev.K.
    Dev.K. almost 9 years
    On 64 bit system, if 32 bit python2.7 is installed..it will return ('32bit', 'WindowsPE')..
  • Vito Gentile
    Vito Gentile almost 9 years
    This is the right answer. It is the only one that works also if you are using Python 32bit on 64bit Windows installation. +1
  • Petri
    Petri over 7 years
    I don't think this is always safe either: You could have a 32-bit OS running on 64-bit processor architecture.
  • Petri
    Petri over 7 years
    Has anyone tested this? Despite the KB title, the KB article actually talks about processor information, so I suspect that the info refers to processor architecture, not the OS version.
  • Petri
    Petri over 7 years
    On Windows7 (64-bit) running in a Parallels VM on OSX, the registry value is 0x00000001 (1 in decimal)
  • Petri
    Petri over 7 years
    On Windows 10 (64-bit), there no longer is such registry attribute ("Platform ID"). But there is a "Platform Specific Field 1", and it's value is 0x00000002 (2 in decimal). Go figure... :(
  • phobie
    phobie over 7 years
    @Petri, in this case all three function correctly return False
  • Petri
    Petri over 7 years
    @phobie, have you tried? I don't have a 32-bit OS on 64-bit hardware, do you?
  • phobie
    phobie over 7 years
    Yes! The underlaying hardware has no impact. The functions have been tested with EM64T systems on all OSs listed by me.
  • RayLuo
    RayLuo about 7 years
    @max The official documentation explains that both the platform.architecture() and the sys.maxsize > 2**32 are trying to detect whether the python intepretor itself is in 32 bit or 64 bit, NOT whether your OS is in 32 bit or 64 bit. Therefore the result you observed was presumably caused by your installation of different builts of Python on those machines. Depending on your purpose, these behavior may or may not be considered a wrong behavior.
  • max
    max about 7 years
    Right. The question does not ask for the interpretor bitness. It mentions the operating system bitness.
  • Jake W
    Jake W almost 7 years
    This is also just telling the arch of python interpreter itself.
  • Nulano
    Nulano about 5 years
    This doesn't return the system platform, but the Python platform, which is what I was looking for.
  • G M
    G M almost 5 years
    no, this is not reliable it returns the architecture of your interpreter
  • 0xC0000022L
    0xC0000022L over 3 years
    @MikeGraham it is not the right solution because whether your program (i.e. in our case the Python interpreter) runs as WOW64 or native 64-bit program governs whether the WOW64 (%SystemRoot%\SysWOW64\cmd.exe) or 64-bit cmd.exe (%SystemRoot%\System32\cmd.exe) gets to run on a 64-bit Windows and thereby makes the output of this value relative to the environment.
  • JojOatXGME
    JojOatXGME about 3 years
    You probably installed the 64-bit version of Python. The issue is that this will not detect an 64-bit OS if you have installed a 32-bit version of Python.
  • CGFoX
    CGFoX about 2 years
    Note that you first have to import platform!