在 Windows 下,我运行的是 32 位 python.exe。我需要知道操作系统/CPU 是 64 位还是 32 位。
我的机器运行的是 Windows7 64 位。
检查 this post ,并尝试运行此 Python 脚本:
import ctypes; print(32 if ctypes.sizeof(ctypes.c_voidp)==4 else 64, 'bit CPU')
import sys; print("%x" % sys.maxsize, sys.maxsize > 2**32)
import struct; print( 8 * struct.calcsize("P"))
import platform; print( platform.architecture()[0] )
print( platform.machine() )
输出:
32 bit CPU
7fffffff False
32
32bit
AMD64
引用帖子中没有任何建议真正为您提供 CPU/OS 架构信息。他们都报告 32 位,因为我正在运行 Python 32 位二进制文件。
我如何以可移植的方式确定 CPU/OS 是 32 位还是 64 位(可以在 platform.machine() 中循环 64 位字符串,但我怀疑这是好方法)?
请您参考如下方法:
您查询的大部分信息是由解释器的字长决定的,而不是由 CPU 决定的。
只有 platform.machine()
会忽略此信息;它取自系统 uname -m
数据,这是确定您的系统是否为 64 位 Linux 的推荐命令和 OS X , Windows 提供完全相同的信息(Python 在所有情况下都使用 C uname()
函数)。
要么测试该字符串中的 64
,要么构建一组可接受的值:
'64' in platform.machine()
或
platform.machine() in {'x86_64', 'AMD64'}