我怎么能从壳内告诉壳处于什么模式?
我已经试着查看了平台模块,但它似乎只告诉您“用于可执行文件的位架构和链接格式”。我的二进制文件被编译为64位(我在OS X 10.6上运行),所以它似乎总是报告64位,即使我使用这里描述的方法强制32位模式。
我怎么能从壳内告诉壳处于什么模式?
我已经试着查看了平台模块,但它似乎只告诉您“用于可执行文件的位架构和链接格式”。我的二进制文件被编译为64位(我在OS X 10.6上运行),所以它似乎总是报告64位,即使我使用这里描述的方法强制32位模式。
当前回答
一种方法是看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)
在Windows操作系统下,执行相同的命令,格式如下:
python -c "import sys;print(\"%x\" % sys.maxsize, sys.maxsize > 2**32)"
sys。maxsize在Python 2.6中引入。如果你需要一个针对旧系统的测试,这个稍微复杂一点的测试应该适用于所有Python 2和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()来实现这一点。不幸的是,它的结果并不总是可靠的,特别是在OS X通用二进制文件的情况下。
$ 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控制台:
import platform
platform.architecture()[0]
它应该显示'64bit'或'32bit'根据您的平台。
或者(对于OS X二进制文件):
import sys
sys.maxsize > 2**32
# it should display True in case of 64bit and False in case of 32bit
在命令行中执行python -VV。它应该返回版本。
C:\Users\xyz>python
Python 2.7.6 (default, Nov XY ..., 19:24:24) **[MSC v.1500 64 bit (AMD64)] on win
32**
Type "help", "copyright", "credits" or "license" for more information.
>>>
在CMD中点击python后
对于32位,它将返回32,对于64位,它将返回64
import struct
print(struct.calcsize("P") * 8)
平台架构不是可靠的方法。 相反,我们:
$ arch -i386 /usr/local/bin/python2.7
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform, sys
>>> platform.architecture(), sys.maxsize
(('64bit', ''), 2147483647)
>>> ^D
$ arch -x86_64 /usr/local/bin/python2.7
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform, sys
>>> platform.architecture(), sys.maxsize
(('64bit', ''), 9223372036854775807)