我怎么能从壳内告诉壳处于什么模式?
我已经试着查看了平台模块,但它似乎只告诉您“用于可执行文件的位架构和链接格式”。我的二进制文件被编译为64位(我在OS X 10.6上运行),所以它似乎总是报告64位,即使我使用这里描述的方法强制32位模式。
我怎么能从壳内告诉壳处于什么模式?
我已经试着查看了平台模块,但它似乎只告诉您“用于可执行文件的位架构和链接格式”。我的二进制文件被编译为64位(我在OS X 10.6上运行),所以它似乎总是报告64位,即使我使用这里描述的方法强制32位模式。
当前回答
试试这个:
import platform
platform.architecture()
其他回答
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后
struct.calcsize("P")返回存储单个指针所需的字节大小。在32位系统上,它将返回4个字节。在64位系统上,它将返回8个字节。
因此,如果你运行的是32位的python,则返回32;如果你运行的是64位的python,则返回64:
Python 2
import struct;print struct.calcsize("P") * 8
Python 3
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)
import sys
print(sys.version)
3.5.1 (v3.5.1:37a07cee5969, 2015年12月6日01:54:25)[MSC .1900 64位(AMD64)]
一种方法是看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