2024-04-26 06:52:25 发布
网友
我需要一种方法从shell内部判断shell处于什么模式。
我试过查看platform模块,但它似乎只告诉您“关于位体系结构和用于可执行文件的链接格式”:二进制文件编译为64位(我在OSX10.6上运行),因此它似乎总是报告64位,即使我使用described here方法强制32位模式)。
基本上是马修·马歇尔答案的变体(std.library的struct):
import struct print struct.calcsize("P") * 8
更新时间: 一种方法是查看文档中的sys.maxsize:
sys.maxsize
$ python-32 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)' ('7fffffff', False) $ python-64 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)' ('7fffffffffffffff', True)
Python 2.6中引入了sys.maxsize。如果您需要对较旧的系统进行测试,这个稍微复杂一点的测试应该适用于所有Python2和3版本:
$ python-32 -c 'import struct;print( 8 * struct.calcsize("P"))' 32 $ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))' 64
顺便说一句,您可能会尝试使用platform.architecture()来实现这一点。不幸的是,它的结果并不总是可靠的,particularly in the case of OS X universal binaries。
platform.architecture()
$ 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
在终端/命令行中启动Python解释器时,您还可能看到一行类似的代码:
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
其中[MSC v.1500 64 bit (AMD64)]表示64位Python。 适用于我的特定设置。
[MSC v.1500 64 bit (AMD64)]
基本上是马修·马歇尔答案的变体(std.library的struct):
更新时间: 一种方法是查看文档中的
sys.maxsize
:Python 2.6中引入了
sys.maxsize
。如果您需要对较旧的系统进行测试,这个稍微复杂一点的测试应该适用于所有Python2和3版本:顺便说一句,您可能会尝试使用
platform.architecture()
来实现这一点。不幸的是,它的结果并不总是可靠的,particularly in the case of OS X universal binaries。在终端/命令行中启动Python解释器时,您还可能看到一行类似的代码:
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
其中
[MSC v.1500 64 bit (AMD64)]
表示64位Python。 适用于我的特定设置。相关问题 更多 >
编程相关推荐