开发者

In Python, how do you determine whether the kernel is running in 32-bit or 64-bit mode?

开发者 https://www.devze.com 2023-03-30 18:32 出处:网络
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'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 loo开发者_开发知识库ks 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)


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())


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


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.


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


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

platform.architecture()[0]

'64bit

0

精彩评论

暂无评论...
验证码 换一张
取 消