我怎么能从壳内告诉壳处于什么模式?
我已经试着查看了平台模块,但它似乎只告诉您“用于可执行文件的位架构和链接格式”。我的二进制文件被编译为64位(我在OS X 10.6上运行),所以它似乎总是报告64位,即使我使用这里描述的方法强制32位模式。
我怎么能从壳内告诉壳处于什么模式?
我已经试着查看了平台模块,但它似乎只告诉您“用于可执行文件的位架构和链接格式”。我的二进制文件被编译为64位(我在OS X 10.6上运行),所以它似乎总是报告64位,即使我使用这里描述的方法强制32位模式。
当前回答
当在终端/命令行中启动Python解释器时,你可能还会看到这样一行:
win32上的Python 2.7.2(默认,2011年6月12日,14:24:46)[MSC v.1500 64位(AMD64)]
其中[MSC v.1500 64位(AMD64)]表示64位Python。 适用于我的特定设置。
其他回答
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后
在我的Centos Linux系统上,我做了以下操作: 1)启动Python解释器(我使用2.6.6) 2)运行如下代码:
import platform
print(platform.architecture())
它给了我
(64bit, 'ELF')
平台架构不是可靠的方法。 相反,我们:
$ 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)
一种方法是看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
基本上是Matthew Marshall答案的变体(使用来自std.library的struct):
import struct
print struct.calcsize("P") * 8