强制使用 32 位 python

发布于 2024-12-13 05:39:36 字数 833 浏览 1 评论 0原文

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

I am having some trouble triggering the 32-bit python for my macports python2.7

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', '')
>>> 

How should I go about triggering the use of 32-bit python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2024-12-20 05:39:36
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 位模式而变化。

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', '')
arch -i386 python

Will run the binary in 32-bit mode (which is what you did).

If you installed Python via MacPorts, check that it was actually built with 32-bit and 64-bit (universal binary).

file `which python`

This is output on mine:

λ > 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

If you don't see the i386, then you your version doesn't have a 32-bit build.

Though if you can run arch -i386 python you should be fine, since you will get an error if your binary can't run 32-bit mode.


Also, do not rely on platform.architecture() to tell you if it's 32-bit, because universal binaries will report 64-bit mode even if you're in 32-bit. It's better to rely on sys.maxsize, which changes depending on if you're in 32-bit or 64-bit mode.

Python in 32-bit mode, notice 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', '')

Python in 64-bit mode:

λ > 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', '')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文