强制使用32位python

2024-06-28 20:02:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在为我的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的使用?在


Tags: defaultappletagsoctnovcompatibleclanggcc
1条回答
网友
1楼 · 发布于 2024-06-28 20:02:25
arch -i386 python

将以32位模式运行二进制文件(这是您所做的)。在

如果您通过MacPorts安装Python,请检查它是否实际上是用32位和64位(通用二进制)构建的。在

^{pr2}$

这是我的输出:

λ > 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位模式。最好依赖^{},这取决于您是处于32位还是64位模式。在

Python在32位模式下,请注意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', '')

相关问题 更多 >