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)

Matt Ball
  • 1,434
  • 2
  • 17
  • 24
  • 1
    possible duplicate of [Detect 64bit OS (windows) in Python](http://stackoverflow.com/questions/2208828/detect-64bit-os-windows-in-python) – Trufa Aug 23 '11 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 '11 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 '11 at 17:30
  • try `platform.architecture()[0]` – Trufa Aug 23 '11 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 '11 at 17:40
  • The accepted answer of [Detect 64bit OS (windows) in Python](http://stackoverflow.com/questions/2208828/) is wrong which has been stated there. The answer of cgohlke is the best one around. – phobie Sep 27 '12 at 11:02

5 Answers5

8

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())
cgohlke
  • 9,142
  • 2
  • 33
  • 36
  • 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 '11 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 '11 at 20:30
  • platform.machine() can return anything the posix command [uname -m](http://en.wikipedia.org/wiki/Uname) 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 '12 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]`. – Dr. Jan-Philip Gehrcke Aug 18 '13 at 15:51
7
>>> import platform
>>> platform.architecture()
('32bit', 'ELF')
Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
  • 1
    Confirmed that this works also Windows `('64bit', 'WindowsPE')` – Kimvais Aug 23 '11 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 '11 at 17:21
  • Are you absolutely positive that you are running 64-bit OS and 64-bit python? – Kimvais Aug 23 '11 at 17:25
  • 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 '11 at 17:26
  • What does `platform.uname()` say for you then? - I get `('Windows', 'VISCOUNT', '7', '6.1.7600', 'AMD64', 'Intel64 Family 6 Model 15 Stepping 13, GenuineIntel')` – Kimvais Aug 23 '11 at 17:28
  • 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 '11 at 17:32
  • I get this, which I think is because I'm running a 32-bit python: python -c "import platform; print platform.uname()" ('Windows', 'MBALL1-W', 'post2008Server', '6.1.7601', 'x86', 'Intel64 Family 6 Model 26 Stepping 5, GenuineIntel ') – Matt Ball Aug 23 '11 at 17:33
  • @Matt if this doesn't work for you, try the maxsize trick: `is_64bits = sys.maxsize > 2**32` – Gabi Purcaru Aug 23 '11 at 19:34
  • 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 '11 at 20:27
5
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.

Santa
  • 11,381
  • 8
  • 51
  • 64
  • When I run this from 32-bit python on 64-bit windows, I get 'x86'. – Matt Ball Aug 23 '11 at 17:47
  • 1
    What version Python? This was "fixed" recently [here](http://bugs.python.org/issue7860). – Santa Aug 23 '11 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 '11 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 '11 at 18:06
  • For the time being, I'm stuck with python 2.6, so I'll probably need to resort to some of those dirty hacks. But thanks for the link to the python 2.7 fix. We may try upgrading if that's not too painful. – Matt Ball Aug 23 '11 at 19:17
  • 1
    platform.machine() is an alias for platform.uname()[4]. Those functions need at least Python 2.7 to work correctly. – phobie Sep 27 '12 at 07: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

rishabhr0y
  • 838
  • 1
  • 9
  • 14
-1

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

platform.architecture()[0]

'64bit

Abeltang
  • 21
  • 1