有谁知道我将如何检测Windows在Python下的位版本。我需要知道这是一种为Program Files使用正确文件夹的方法。
非常感谢
答案 0 :(得分:42)
我认为问题的最佳解决方案已由Mark Ribau发布。
Python 2.7及更新版本问题的最佳答案是:
def is_os_64bit():
return platform.machine().endswith('64')
在Windows上,跨平台函数platform.machine()
在内部使用Matthew Scoutens答案中使用的环境变量。
我找到了以下值:
对于Python 2.6及更早版本:
def is_windows_64bit():
if 'PROCESSOR_ARCHITEW6432' in os.environ:
return True
return os.environ['PROCESSOR_ARCHITECTURE'].endswith('64')
要查找我使用的Python解释器位版本:
def is_python_64bit():
return (struct.calcsize("P") == 8)
答案 1 :(得分:39)
我猜你应该在os.environ['PROGRAMFILES']
查看程序文件文件夹。
答案 2 :(得分:29)
来到这里寻找正确检测是否在64位窗口上运行,将上述所有内容编译成更简洁的内容。
下面你将找到一个测试是否在64位窗口上运行的函数,一个获取32位Program Files文件夹的函数,以及一个获取64位Program Files文件夹的函数;所有无论运行32位还是64位python。运行32位python时,大多数事情在64位运行时报告为32位,甚至是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
注意:是的,这有点hackish。所有其他“应该正常工作”的方法在64位Windows上运行32位Python时不起作用(至少对于我尝试过的各种2.x和3.x版本)。
编辑:
2011-09-07 - 添加了一个关于为什么只有这种hackish方法正常工作的说明。
答案 3 :(得分:21)
platform
module - 访问底层平台的识别数据
>>> import platform
>>> platform.architecture()
('32bit', 'WindowsPE')
在64位Windows上,32位Python返回:
('32bit', 'WindowsPE')
这意味着这个答案即使已被接受,也是不正确的。请参阅下面的一些答案,了解可能适用于不同情况的选项。
答案 4 :(得分:8)
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
答案 5 :(得分:6)
许多这些提议的解决方案,例如platform.architecture(),都会失败,因为它们的结果取决于您运行的是32位还是64位Python。
我找到的唯一可靠的方法是检查是否存在os.environ ['PROGRAMFILES(X86)'],这是不幸的hackish。
答案 6 :(得分:3)
您应该使用环境变量来访问它。程序文件目录存储在x86 Windows上的环境变量PROGRAMFILES
中,32位程序文件目录存储在PROGRAMFILES(X86)
环境变量中,可以使用os.environ('PROGRAMFILES')
访问这些目录。
使用sys.getwindowsversion()
或PROGRAMFILES(X86)
(if 'PROGRAMFILES(X86)' in os.environ
)的存在来确定您正在使用的Windows版本。
答案 7 :(得分:2)
我刚刚找到了另一种方法,这在某些情况下可能会有用。
import subprocess
import os
def os_arch():
os_arch = '32-bit'
if os.name == 'nt':
output = subprocess.check_output(['wmic', 'os', 'get', 'OSArchitecture'])
os_arch = output.split()[1]
else:
output = subprocess.check_output(['uname', '-m'])
if 'x86_64' in output:
os_arch = '64-bit'
else:
os_arch = '32-bit'
return os_arch
print 'os_arch=%s' % os_arch()
我在以下环境中测试了此代码:
答案 8 :(得分:2)
关注this documentation,请尝试以下代码:
is_64bits = sys.maxsize > 2**32
答案 9 :(得分:2)
import platform
platform.architecture()[0]
它将返回'32位'或'64位',具体取决于系统架构。
答案 10 :(得分:1)
我知道在问题的评论中已经使用了这种方法。 这是.net framework uses:
的方法import ctypes
def is64_bit_os():
""" Returns wethever system is a 64bit operating system"""
is64bit = ctypes.c_bool()
handle = ctypes.windll.kernel32.GetCurrentProcess() # should be -1, because the current process is currently defined as (HANDLE) -1
success = ctypes.windll.kernel32.IsWow64Process(handle, ctypes.byref(is64bit)) #should return 1
return (success and is64bit).value
print(is64_bit_os())
答案 11 :(得分:1)
只是为了更新这个旧线程 - 看起来平台模块现在报告了正确的架构(至少在Python 2.7.8中):
c:\python27\python.exe -c "import platform; print platform.architecture(), platform.python_version()"
('32bit', 'WindowsPE') 2.7.6
c:\home\python278-x64\python.exe -c "import platform; print platform.architecture(), platform.python_version()"
('64bit', 'WindowsPE') 2.7.8
(对不起,我没有代表对仍然声称错误的第一个答案发表评论:)
答案 12 :(得分:1)
当您需要了解有关Windows系统的内容时,它通常位于注册表中的某个位置,根据MS文档,您应该查看(http://support.microsoft.com/kb/556009)此键值:
<强> HKLM \ HARDWARE \ DESCRIPTION \ SYSTEM \ CentralProcessor \ 0 强>
如果是:
0x00000020(十进制32)
这是一台32位机器。
答案 13 :(得分:1)
主题行询问有关检测64或32位操作系统的问题,而正文则讨论确定ProgramFiles的位置。后者在这里有几个可行的答案。我想添加另一个通用的解决方案来处理StartMenu,桌面等以及ProgramFiles:How to get path of Start Menu's Programs directory?
答案 14 :(得分:0)
AlexanderBrüsch发布的解决方案是正确的解决方案,但是它存在一个仅在python3.x上显示出来的错误。他忽略了将返回的值从GetCurrentProcess()转换为HANDLE类型。传递简单整数作为IsWow64Process()的第一个参数将返回0(这是win32api的错误标志)。另外,Alexander错误地处理了return语句(成功没有.value
属性)。
对于那些迷失于此线程的人,这是更正后的代码:
import ctypes
def is64_bit_os():
"""Returns True if running 32-bit code on 64-bit operating system"""
is64bit = ctypes.c_bool()
handle = ctypes.wintypes.HANDLE(ctypes.windll.kernel32.GetCurrentProcess())
success = ctypes.windll.kernel32.IsWow64Process(handle, ctypes.byref(is64bit))
return success and is64bit.value
print(is64_bit_os())
答案 15 :(得分:0)
machine
模块中有一个名为 platform
的函数。我在装有 64 位 Windows 10 的同一台 64 位机器上安装了 Python3.8 32 位和 64 位版本,这是我发现的:
而且它看起来像 platform.machine
返回机器架构而无需打扰安装什么类型的 python。所以这是我的
最终编译
import platform
def is_64bit():
return platform.machine().endswith('64')
答案 16 :(得分:0)
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=cputype = get_registry_value(
"HKEY_LOCAL_MACHINE",
"SYSTEM\\CurrentControlSet\Control\\Session Manager\\Environment",
"PROCESSOR_ARCHITECTURE")
print windowsbit
只需运行此代码
如果您正在使用64位Windows机器,则会打印AMD64
或者如果您正在使用32位,它将打印AMD32
我希望这段代码可以帮助完全解决这个问题
答案 17 :(得分:0)
import struct
def is64Windows():
return struct.calcsize('P') * 8 == 64
答案 18 :(得分:0)
这适用于我使用的Python版本:2.7和2.5.4
import win32com.client
import _winreg
shell = win32com.client.Dispatch('WScript.Shell')
proc_arch = shell.ExpandEnvironmentStrings(r'%PROCESSOR_ARCHITECTURE%').lower()
if proc_arch == 'x86':
print "32 bit"
elif proc_arch == 'amd64':
print "64 bit"
else:
raise Exception("Unhandled arch: %s" % proc_arch)
答案 19 :(得分:0)
64位版本的Windows使用称为注册表重定向和反射键的东西。有一个名为WoW64的兼容层,它支持32位应用程序的兼容性。从Windows 7和Windows Server 2008 R2开始,WoW64注册表项不再反映,而是共享。你可以在这里阅读:
registry-reflection:msdn.microsoft.com/en-us/library/aa384235(v = vs.85).aspx
affected-keys:msdn.microsoft.com/en-us/library/aa384253(v = vs.85).aspx
维基百科:en.wikipedia.org/wiki/WoW64
您需要做的就是检测这些键的存在。您可以使用_winreg。使用try:并尝试打开键,例如:
try:
aReg = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")
答案 20 :(得分:-1)
在Windows 64bit下应该有一个目录,一个名为\Windows\WinSxS64
的64位文件夹,在Windows 32bit下,它是WinSxS。