13

I'm running python 2.6 on Linux, Mac OS, and Windows, and need to determine whether the kernel is running in 32-bit or 64-bit mode. Is there an easy way to do this?

I've looked at platform.machine(), but this doesn't work properly on Windows.

I've also looked at platform.architecture(), and this doesn't work when running 32-bit python on 64-bit Windows.

Note: It looks like python 2.7 has a fix that makes platform.architecture() work correctly. Unfortunately, I need to use python 2.6 (at least for now).

(edit: From talking to folks off-line, it sounds like there probably isn't a robust python-only way to make this determination without resorting to evil hacks. I'm just curious what evil hacks folks have used in their projects that use python 2.6. For example, on Windows it may be necessary to look at the PROCESSOR_ARCHITEW6432 environment variable and check for AMD64)

6
  • 1
    possible duplicate of Detect 64bit OS (windows) in Python
    – Trufa
    Aug 23, 2011 at 17:19
  • 1
    See the comments in the accepted answer, this may or may not be as straightforward in a 64 bit OS.
    – Trufa
    Aug 23, 2011 at 17:20
  • The accepted answer for "Detect 64bit OS (windows) in Python" doesn't work for me, so it's not quite a duplicate.
    – Matt Ball
    Aug 23, 2011 at 17:30
  • try platform.architecture()[0]
    – Trufa
    Aug 23, 2011 at 17:35
  • When running platform.architecture()[0] from 32-bit python on 64-bit windows, I get '32bit'. This seems to be telling me more about python itself than the kernel that's running.
    – Matt Ball
    Aug 23, 2011 at 17:40

5 Answers 5

9

How about working around issue7860

import os
import sys
import platform

def machine():
    """Return type of machine."""
    if os.name == 'nt' and sys.version_info[:2] < (2,7):
        return os.environ.get("PROCESSOR_ARCHITEW6432", 
               os.environ.get('PROCESSOR_ARCHITECTURE', ''))
    else:
        return platform.machine()

def os_bits(machine=machine()):
    """Return bitness of operating system, or None if unknown."""
    machine2bits = {'AMD64': 64, 'x86_64': 64, 'i386': 32, 'x86': 32}
    return machine2bits.get(machine, None)

print (os_bits())
4
  • 1
    Thanks a bunch for the solution! I think we're almost there! This solution is good, except that it returns strings like 'i386', 'x86_64', 'AMD64', and 'x86', which makes it hard to quickly determine whether the kernel is running in 32-bit or 64-bit mode. Maybe it would be possible to do something like return 64 if the architecture has a '64' in it, and to otherwise return 32. I don't know off-hand if this solution covers all cases, but it does cover the ones I've seen so far. Example: return 64 if '64' in previous_return_value else 32
    – Matt Ball
    Aug 24, 2011 at 19:44
  • Thanks for doing the extra work! I think this one gets the money, although I'd be curious if anyone else knows about other possible values for platform.machine() or PROCESSOR_ARCHITECTURE/PROCESSOR_ARCHITEW6432.
    – Matt Ball
    Aug 24, 2011 at 20:30
  • platform.machine() can return anything the posix command uname -m returns. PROCESSOR_ARCHITECTURE can be AMD64, IA64 or x86. I think PROCESSOR_ARCHITEW6432 can only be AMD64 or empty. There might be new values for Windows on ARM.
    – phobie
    Sep 27, 2012 at 11:15
  • Okay, and since Python >= 2.7 is pretty standard these days, we can use platform.machine() directly and use your translation dictionary. FWIW, according to Python 2.7's platform module's source, platform.machine() is equivalent to platform.uname()[4]. Aug 18, 2013 at 15:51
6
>>> import platform
>>> platform.architecture()
('32bit', 'ELF')
9
  • 1
    Confirmed that this works also Windows ('64bit', 'WindowsPE')
    – Kimvais
    Aug 23, 2011 at 17:13
  • 1
    No dice: I just tried this on my 64-bit Windows 7 box and got this result: python -c "import platform; print platform.architecture()" ('32bit', 'WindowsPE')
    – Matt Ball
    Aug 23, 2011 at 17:21
  • 3
    I'm running 32-bit python on 64-bit windows (My env has this: PROCESSOR_ARCHITEW6432=AMD64). In this context, it doesn't matter whether python itself is 32/64-bit, it's the kernel I'm after.
    – Matt Ball
    Aug 23, 2011 at 17:26
  • 1
    On my Mac, I get for Python 2.5 ('32bit', '') and for Python 2.6 ('64bit', ''). So this code gives you only the mode of the Python Interpreter, not the kernel.
    – jazz
    Aug 23, 2011 at 17:32
  • 1
    @Gabi: Thank you for the attempt, but unfortunately this trick only indicates whether python was compiled for 32-bit or 64-bit. On my 64-bit Windows system with 32-bit python 2.6, I get false when running this code.
    – Matt Ball
    Aug 23, 2011 at 20:27
3
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.uname()[4]
'AMD64'

This is with Python 32-bit on a 64-bit Windows OS.

6
  • When I run this from 32-bit python on 64-bit windows, I get 'x86'.
    – Matt Ball
    Aug 23, 2011 at 17:47
  • 1
    What version Python? This was "fixed" recently here.
    – Santa
    Aug 23, 2011 at 17:48
  • I'm running python 2.6: ActivePython 2.6.2.2 (ActiveState Software Inc.) based on Python 2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 bit (Intel)] on win32
    – Matt Ball
    Aug 23, 2011 at 17:50
  • 1
    That's why it didn't work. The "fix" only went into the 2.7+ branches. Prior to that, the built-in functions could only query Python's architecture, not the underlying OS' (unless you use libraries like pywin32 to call directly into the OS, or other dirty hacks).
    – Santa
    Aug 23, 2011 at 18:06
  • 1
    platform.machine() is an alias for platform.uname()[4]. Those functions need at least Python 2.7 to work correctly.
    – phobie
    Sep 27, 2012 at 7:51
0

i hope this can solve the problem i tried it on my windows 8.1 64 bit and returns the value AMD64 for me

import _winreg
def get_registry_value(key, subkey, value):

  key = getattr(_winreg, key)
  handle = _winreg.OpenKey(key, subkey )
  (value, type) = _winreg.QueryValueEx(handle, value)
  return value

windowsbit = get_registry_value(
"HKEY_LOCAL_MACHINE",
"SYSTEM\\CurrentControlSet\Control\\Session Manager\\Environment",
"PROCESSOR_ARCHITECTURE")
print windowsbit

just run this code if you are working on 64 bit windows machine this will print AMD64

or if you are working on 32 bit it will print x86

i hope this code can help to solve this problem fully

2
  • Why was this downvoted? It seems to me that it solves the problem. Mar 16, 2020 at 20:10
  • I did : ) I hope it helps. Mar 20, 2020 at 16:52
-1

we can used follow API to detect current is 32bit or 64 bit

platform.architecture()[0]

'64bit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.