I need a way to tell what mode the shell is in from within the shell.
我需要一种方法来分辨出壳内的壳层。
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).
我试着看平台模块但似乎只告诉你关于一些架构和链接格式用于执行”:编译的二进制为64位虽然(我运行OS X 10.6)所以它似乎总是报告64位尽管我使用这里描述的方法迫使32位模式)。
12 个解决方案
#1
324
UPDATED: One way is to look at sys.maxsize
as documented here:
更新:一种方法是看sys。最大尺寸的记录:
$ 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)
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:
sys。在Python 2.6中引入了maxsize。如果您需要对旧系统进行测试,那么这个稍微复杂一点的测试应该适用于所有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
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.
顺便说一下,您可能想要使用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
#2
201
When starting the Python interpreter in the terminal/command line you may also see a line like:
当在终端/命令行启动Python解释器时,您可能还会看到一行如下:
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Python 2.7.2(默认,2011年6月12日,14:24:46)[MSC v。150064位(AMD64)]在win32上。
Where [MSC v.1500 64 bit (AMD64)]
means 64-bit Python. Works for my particular setup.
(MSC v。1500 64位(AMD64)]表示64位Python。为我的特殊设置工作。
#3
148
Basically a variant on Matthew Marshall's answer (with struct from the std.library):
马修·马歇尔的答案(与std.library的结构有关)的一个变体:
import struct
print struct.calcsize("P") * 8
#4
58
Try using ctypes to get the size of a void pointer:
尝试使用ctypes来获得空指针的大小:
import ctypes
print ctypes.sizeof(ctypes.c_voidp)
It'll be 4 for 32 bit or 8 for 64 bit.
它是4,32位或8位,64位。
#5
26
Open python console:
打开python控制台:
import platform
platform.architecture()[0]
it should display the '64bit' or '32bit' according to your platform.
根据你的平台,它应该显示“64位”或“32位”。
Alternatively( in case of OS X binaries ):
或者(在OS X二进制文件中):
import sys
sys.maxsize > 2**32
# it should display True in case of 64bit and False in case of 32bit
#6
14
For a non-programmatic solution, look in the Activity Monitor. It lists the architecture of 64-bit processes as “Intel (64-bit)”.
对于非编程解决方案,请查看活动监视器。它将64位流程的架构称为“Intel(64位)”。
#7
12
On my Centos Linux system I did the following:
1) Started the Python interpreter (I'm using 2.6.6)
2) Ran the following code:
在我的Centos Linux系统上,我做了如下工作:1)启动Python解释器(我使用2.6.6)2)运行以下代码:
import platform
print(platform.architecture())
and it gave me
它给了我
(64bit, 'ELF')
#8
8
platform.architecture()
notes say:
platform.architecture()笔记说:
Note: On Mac OS X (and perhaps other platforms), executable files may be universal files containing multiple architectures.
注意:在Mac OS X(或者其他平台)上,可执行文件可能是包含多个架构的通用文件。
To get at the “64-bitness” of the current interpreter, it is more reliable to query the sys.maxsize attribute:
为了得到当前解释器的“64位”,查询sys更可靠。最大尺寸属性:
import sys
is_64bits = sys.maxsize > 2**32
#9
6
struct.calcsize("P")
returns size of the bytes required to store a single pointer. On a 32-bit system, it would return 4 bytes. On a 64-bit system, it would return 8 bytes.
calcsize(“P”)返回存储单个指针所需的字节大小。在32位系统上,它将返回4个字节。在64位系统上,它将返回8个字节。
So the following would return 32
if you're running 32-bit python and 64
if you're running 64-bit python:
如果你运行的是32位的python和64位,如果你运行的是64位的python,下面将返回32。
Python 2
Python 2
import struct;print struct.calcsize("P") * 8
Python 3
Python 3
import struct;print(struct.calcsize("P") * 8)
#10
3
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.
>>>
after hitting python in cmd
在cmd中攻击python之后。
#11
2
import sys
print(sys.version)
3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)]
3.5.1(2015年12月6日,01:54:25)[MSC v]。1900 64位(AMD64))
#12
0
Platform Architecture is not the reliable way. Instead us:
平台架构不是可靠的方法。相反,我们:
$ 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)
#1
324
UPDATED: One way is to look at sys.maxsize
as documented here:
更新:一种方法是看sys。最大尺寸的记录:
$ 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)
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:
sys。在Python 2.6中引入了maxsize。如果您需要对旧系统进行测试,那么这个稍微复杂一点的测试应该适用于所有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
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.
顺便说一下,您可能想要使用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
#2
201
When starting the Python interpreter in the terminal/command line you may also see a line like:
当在终端/命令行启动Python解释器时,您可能还会看到一行如下:
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Python 2.7.2(默认,2011年6月12日,14:24:46)[MSC v。150064位(AMD64)]在win32上。
Where [MSC v.1500 64 bit (AMD64)]
means 64-bit Python. Works for my particular setup.
(MSC v。1500 64位(AMD64)]表示64位Python。为我的特殊设置工作。
#3
148
Basically a variant on Matthew Marshall's answer (with struct from the std.library):
马修·马歇尔的答案(与std.library的结构有关)的一个变体:
import struct
print struct.calcsize("P") * 8
#4
58
Try using ctypes to get the size of a void pointer:
尝试使用ctypes来获得空指针的大小:
import ctypes
print ctypes.sizeof(ctypes.c_voidp)
It'll be 4 for 32 bit or 8 for 64 bit.
它是4,32位或8位,64位。
#5
26
Open python console:
打开python控制台:
import platform
platform.architecture()[0]
it should display the '64bit' or '32bit' according to your platform.
根据你的平台,它应该显示“64位”或“32位”。
Alternatively( in case of OS X binaries ):
或者(在OS X二进制文件中):
import sys
sys.maxsize > 2**32
# it should display True in case of 64bit and False in case of 32bit
#6
14
For a non-programmatic solution, look in the Activity Monitor. It lists the architecture of 64-bit processes as “Intel (64-bit)”.
对于非编程解决方案,请查看活动监视器。它将64位流程的架构称为“Intel(64位)”。
#7
12
On my Centos Linux system I did the following:
1) Started the Python interpreter (I'm using 2.6.6)
2) Ran the following code:
在我的Centos Linux系统上,我做了如下工作:1)启动Python解释器(我使用2.6.6)2)运行以下代码:
import platform
print(platform.architecture())
and it gave me
它给了我
(64bit, 'ELF')
#8
8
platform.architecture()
notes say:
platform.architecture()笔记说:
Note: On Mac OS X (and perhaps other platforms), executable files may be universal files containing multiple architectures.
注意:在Mac OS X(或者其他平台)上,可执行文件可能是包含多个架构的通用文件。
To get at the “64-bitness” of the current interpreter, it is more reliable to query the sys.maxsize attribute:
为了得到当前解释器的“64位”,查询sys更可靠。最大尺寸属性:
import sys
is_64bits = sys.maxsize > 2**32
#9
6
struct.calcsize("P")
returns size of the bytes required to store a single pointer. On a 32-bit system, it would return 4 bytes. On a 64-bit system, it would return 8 bytes.
calcsize(“P”)返回存储单个指针所需的字节大小。在32位系统上,它将返回4个字节。在64位系统上,它将返回8个字节。
So the following would return 32
if you're running 32-bit python and 64
if you're running 64-bit python:
如果你运行的是32位的python和64位,如果你运行的是64位的python,下面将返回32。
Python 2
Python 2
import struct;print struct.calcsize("P") * 8
Python 3
Python 3
import struct;print(struct.calcsize("P") * 8)
#10
3
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.
>>>
after hitting python in cmd
在cmd中攻击python之后。
#11
2
import sys
print(sys.version)
3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)]
3.5.1(2015年12月6日,01:54:25)[MSC v]。1900 64位(AMD64))
#12
0
Platform Architecture is not the reliable way. Instead us:
平台架构不是可靠的方法。相反,我们:
$ 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)