Python位数可通过platform.architecture()直接判断,如('64bit', 'WindowsPE')为64位;struct.calcsize("P")返回8为64位、4为32位;sys.maxsize为9223372036854775807则为64位。
直接在Python交互环境中运行一行代码就能确认:查看 platform.architecture() 或 struct.calcsize("P") 的结果。
这是最直观、跨平台的方式:
python 或 python3)import platform
print(platform.architecture())
输出类似 ('64bit', 'WindowsPE') 表示 64 位,('32bit', 'WindowsPE') 表示 32 位。括号中第一个值就是关键。
原理是:指针长度直接反映系统位数(32 位系统指针占 4 字节,64 位占 8 字节):
立即学习“Python免费学习笔记(深入)”;
import struct
print(struct.calcsize("P"))
结果为 4 → 32 位;结果为 8 → 64 位。
这个值能间接反映地址空间大小:
import
sys; print(sys.maxsize)
9223372036854775807(即 2⁶³−1)→ 64 位2147483647(即 2³¹−1)→ 32 位Python 的位数取决于它被编译时的目标平台,不一定和当前 Windows/macOS 是同一架构。例如:64 位 Windows 上可以安装并运行 32 位 Python。所以务必查 Python 自身,而不是系统。