我需要一种方法,从外壳程序中告诉外壳程序处于哪种模式。
我尝试查看平台模块,但似乎只告诉您“有关可执行程序所使用的位架构和链接格式”的信息:尽管二进制文件编译为64位(我在OS X 10.6上运行),所以即使我使用此处介绍的方法强制使用32位模式,它似乎总是报告64位。
#1楼
在终端/命令行中启动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。 适用于我的特定设置。
#2楼
更新:一种方法是看看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)
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
#3楼
尝试使用ctypes获得void指针的大小:
import ctypes
print ctypes.sizeof(ctypes.c_voidp)
对于32位将是4,对于64位将是8。
#4楼
基本上是马修·马歇尔(Matthew Marshall)回答的一个变体(带有来自标准库的结构):
import struct
print struct.calcsize("P") * 8
#5楼
对于非编程解决方案,请查看活动监视器。 它将64位进程的体系结构列为“ Intel(64位)”。
#6楼
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后
#7楼
在我的Centos Linux系统上,我执行了以下操作:
1)启动Python解释器(我正在使用2.6.6)
2)运行以下代码:
import platform
print(platform.architecture())
它给了我
(64bit, 'ELF')
#8楼
注意:在Mac OS X(可能还有其他平台)上,可执行文件可能是包含多种体系结构的通用文件。
为了获得当前解释器的“ 64位”,查询sys.maxsize属性更加可靠:
import sys
is_64bits = sys.maxsize > 2**32
#9楼
打开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
#10楼
struct.calcsize("P")
返回存储单个指针所需的字节大小。 在32位系统上,它将返回4个字节。 在64位系统上,它将返回8个字节。
所以下面将返回32
,如果你正在运行的32位Python和64
,如果你正在运行的64位蟒蛇:
Python 2
import struct;print struct.calcsize("P") * 8
Python 3
import struct;print(struct.calcsize("P") * 8)
#11楼
import sys
print(sys.version)
3.5.1(v3.5.1:37a07cee5969,2015年12月6日,01:54:25)[MSC v.1900 64位(AMD64) ]
#12楼
平台架构不是可靠的方法。 相反,我们:
$ 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)
#13楼
分组一切...
考虑到:
- 询问OSX的问题(我有一个旧的(破解的) VM,带有一个古老的Python版本)
- 我的主要环境是Win
- 我在Win上仅安装了32位版本(并且我在Lnx上构建了一个“残缺的” 版本 )
我将使用Python 3和Python 2在所有3个平台上进行示例。
- 检查[Python 3.Docs]:sys。 maxsize值-将其与
0x100000000
(2 ** 32
)比较:对于64位 ,较大,对于32 位 ,较小:- OSX 9 x64 :
- Python 2.7.10 x64 :
>>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 2.7.10 (default, Oct 14 2015, 05:51:29) \\n[GCC 4.8.2] on darwin' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffffffffffff', True)
- Python 2.7.10 x64 :
- Ubuntu 16 x64 :
- Python 3.5.2 x64 :
>>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.5.2 (default, Nov 23 2017, 16:37:01) \\n[GCC 5.4.0 20160609] on linux' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffffffffffff', True)
- Python 3.6.4 x86 :
>>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.6.4 (default, Apr 25 2018, 23:55:56) \\n[GCC 5.4.0 20160609] on linux' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffff', False)
- Python 3.5.2 x64 :
- 赢10 x64 :
- Python 3.5.4 x64 :
>>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffffffffffff', True)
- Python 3.6.2 x86 :
>>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffff', False)
- Python 3.5.4 x64 :
- OSX 9 x64 :
- 使用[Python 3.Docs]:结构。 calcsize ( format )确定(指针)格式产生的对象大小。 换句话说,确定指针大小(
sizeof(void*)
):- OSX 9 x64 :
- Python 2.7.10 x64 :
>>> import struct >>> struct.calcsize("P") * 8 64
- Python 2.7.10 x64 :
- Ubuntu 16 x64 :
- Python 3.5.2 x64 :
>>> import struct >>> struct.calcsize("P") * 8 64
- Python 3.6.4 x86 :
>>> import struct >>> struct.calcsize("P") * 8 32
- Python 3.5.2 x64 :
- 赢10 x64 :
- Python 3.5.4 x64 :
>>> import struct >>> struct.calcsize("P") * 8 64
- Python 3.6.2 x86 :
>>> import struct >>> struct.calcsize("P") * 8 32
- Python 3.5.4 x64 :
- OSX 9 x64 :
- 使用[Python 3.Docs]:ctypes-Python的外部函数库 。 它还归结为确定指针的大小(
sizeof(void*)
)。 注意, ctypes使用#2。 (不一定要完成此任务),通过“ $ {PYTHON_SRC_DIR} / Lib / ctypes / __ init __。py” (在第15行附近):- OSX 9 x64 :
- Python 2.7.10 x64 :
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
- Python 2.7.10 x64 :
- Ubuntu 16 x64 :
- Python 3.5.2 x64 :
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
- Python 3.6.4 x86 :
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32
- Python 3.5.2 x64 :
- 赢10 x64 :
- Python 3.5.4 x64 :
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
- Python 3.6.2 x86 :
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32
- Python 3.5.4 x64 :
- OSX 9 x64 :
- [Python 3.Docs]:平台。 架构 ( 可执行文件= sys.executable,位='',链接='' ) !!! 在OSX上不可靠! 由于采用多体系结构可执行文件(或.dylib )格式(在某些情况下,使用#2。 ):
- OSX 9 x64 :
- Python 2.7.10 x64 :
>>> import platform >>> platform.architecture() ('64bit', '')
- Python 2.7.10 x64 :
- Ubuntu 16 x64 :
- Python 3.5.2 x64 :
>>> import platform >>> platform.architecture() ('64bit', 'ELF')
- Python 3.6.4 x86 :
>>> import platform >>> platform.architecture() ('32bit', 'ELF')
- Python 3.5.2 x64 :
- 赢10 x64 :
- Python 3.5.4 x64 :
>>> import platform >>> platform.architecture() ('64bit', 'WindowsPE')
- Python 3.6.2 x86 :
>>> import platform >>> platform.architecture() ('32bit', 'WindowsPE')
- Python 3.5.4 x64 :
- OSX 9 x64 :
- me脚的解决方法( gainarie )-通过[Python 3.Docs]:os调用外部命令( [man7]:FILE(1) ) 。 系统 ( 命令 ) 。 #4的局限性。 适用(有时甚至无法使用):
- OSX 9 x64 :
- Python 2.7.10 x64 :
>>> import os >>> os.system("file {0:s}".format(os.path.realpath(sys.executable))) /opt/OPSWbuildtools/2.0.6/bin/python2.7.global: Mach-O 64-bit executable x86_64
- Python 2.7.10 x64 :
- Ubuntu 16 x64 :
- Python 3.5.2 x64 :
>>> import os >>> os.system("file {0:s}".format(os.path.realpath(sys.executable))) /usr/bin/python3.5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=59a8ef36ca241df24686952480966d7bc0d7c6ea, stripped
- Python 3.6.4 x86 :
>>> import os >>> os.system("file {0:s}".format(os.path.realpath(sys.executable))) /home/cfati/Work/Dev/Python-3.6.4/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=5c3d4eeadbd13cd91445d08f90722767b0747de2, not stripped
- Python 3.5.2 x64 :
- 赢10 x64 :
- 文件工具不存在,还有其他可以使用的第三方工具,但是我不会坚持使用它们
- OSX 9 x64 :
具体赢 :
- 通过[Python 3.Docs]检查环境变量(例如%PROCESSOR_ARCHITECTURE% (或其他)) 。 环境 :
- 赢10 x64 :
- Python 3.5.4 x64 :
>>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'AMD64'
- Python 3.6.2 x86 :
>>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'x86'
- Python 3.5.4 x64 :
- 赢10 x64 :
- [Python 3.Docs]:sys。 版本 (启动解释时也显示在第一行 )
- 检查#1。
#14楼
platform.architecture()
有问题(且昂贵)。
从Py2.6开始,方便地测试sys.maxsize > 2**32
。
这是对实际(默认)指针大小的可靠测试,并且至少从Py2.3: struct.calcsize('P') == 8
开始,它是兼容的。 另外: ctypes.sizeof(ctypes.c_void_p) == 8
。
注意:可以使用gcc选项-mx32
或-mx32
版本进行构建,它们是64位体系结构的应用程序,但默认使用32位指针(节省内存和速度)。 'sys.maxsize = ssize_t'可能不严格表示C指针大小(无论如何通常为2**31 - 1
31-1)。 而且,有些系统的代码和数据的指针大小不同,因此需要澄清辨别“ 32位或64位模式”的目的是什么?