强制使用32位Python

0 投票
1 回答
1575 浏览
提问于 2025-04-17 05:21

我在使用macports的python2.7时,遇到了一些问题,想要启动32位的python。

calvins-MacBook ttys003 Tue Nov 01 01:04:23 |~|
calvin$ arch -arch x86_64 python
Python 2.7.2 (default, Oct 31 2011, 20:10:35) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.10.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform; platform.architecture()
('64bit', '')
>>> exit()
calvins-MacBook ttys003 Tue Nov 01 01:04:49 |~|
calvin$ arch -arch i386 python
Python 2.7.2 (default, Oct 31 2011, 20:10:35) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.10.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform; platform.architecture()
('64bit', '')
>>> 

我该怎么做才能启动32位的python呢?

1 个回答

3
arch -i386 python

这会以32位模式运行程序(这就是你所做的)。

如果你是通过MacPorts安装的Python,检查一下它是否真的支持32位和64位(即通用二进制)。

file `which python`

这是我这边的输出:

λ > file /usr/local/bin/python
/usr/local/bin/python: Mach-O universal binary with 2 architectures
/usr/local/bin/python (for architecture i386):  Mach-O executable i386
/usr/local/bin/python (for architecture x86_64):    Mach-O 64-bit executable x86_64

如果你没有看到 i386,那么你的版本就没有32位的支持。

不过如果你能运行 arch -i386 python,那就没问题,因为如果你的程序不能以32位模式运行,会报错的。


另外,不要依赖 platform.architecture() 来判断是否是32位,因为通用二进制会在32位模式下也报告64位。更好的方法是查看 sys.maxsize,这个值会根据你是32位还是64位而变化。

在32位模式下的Python,注意 sys.maxsize > 2**32

λ > arch -i386 python
Python 2.7.2 (default, Oct 31 2011, 00:51:07) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.maxsize > 2**32
False
>>> sys.maxsize
2147483647
>>> import platform
>>> platform.architecture()
('64bit', '')

在64位模式下的Python:

λ > python
Python 2.7.2 (default, Oct 31 2011, 00:51:07) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.maxsize
9223372036854775807
>>> sys.maxsize > 2**32
True
>>> import platform
>>> platform.architecture()
('64bit', '')

撰写回答