如何确定我的python shell是在OS X上以32位还是64位模式执行的?

如何确定我的python shell是在OS X上以32位还是64位模式执行的?

问题描述:

我需要一种方法,可以从shell内分辨出shell所处的模式.

I need a way to tell what mode the shell is in from within the shell.

我尝试查看平台模块,但这似乎只是在告诉您关于关于可执行文件所用的位体系结构和链接格式"的信息:尽管二进制文件被编译为64位(我在OS X 10.6上运行),所以即使我使用的方法此处所述强制32位模式)

I've tried looking at the platform module but it seems only to tell you about "about the bit architecture and the linkage format used for the executable": the binary is compiled as 64bit though (I'm running on OS X 10.6) so it seems to always report 64bit even though I'm using the methods described here to force 32bit mode).

一种方法是按照

sys.maxsize是Python 2.6中引入的.如果您需要针对较旧系统的测试,则此稍微复杂一些的测试应适用于所有Python 2和3版本:

sys.maxsize was introduced in Python 2.6. If you need a test for older systems, this slightly more complicated test should work on all Python 2 and 3 releases:

$ python-32 -c 'import struct;print( 8 * struct.calcsize("P"))'
32
$ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))'
64

顺便说一句,您可能很想为此使用platform.architecture().不幸的是,它的结果并不总是可靠的,尤其是在OS X通用二进制文件中.

BTW, you might be tempted to use platform.architecture() for this. Unfortunately, its results are not always reliable, particularly in the case of OS X universal binaries.

$ arch -x86_64 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit True
$ arch -i386 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit False