如何确定我的 python shell 是在 32 位还是 64 位执行?
- 2024-12-11 08:47:00
- admin 原创
- 130
问题描述:
我怎样才能从 shell 内部判断 shell 处于什么模式?
我尝试查看平台模块,但它似乎只告诉您有关“可执行文件使用的位体系结构和链接格式”的信息。我的二进制文件编译为 64 位(我在 OS X 10.6 上运行),因此即使我使用此处描述的方法强制使用 32 位模式,它似乎也总是报告 64 位。
解决方案 1:
一种方法是查看此处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
解决方案 2:
当在终端/命令行启动 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。适用于我的特定设置。
解决方案 3:
基本上是 Matthew Marshall 答案的一个变体(使用来自 std.library 的结构):
import struct
print struct.calcsize("P") * 8
解决方案 4:
尝试使用 ctypes 来获取 void 指针的大小:
import ctypes
print ctypes.sizeof(ctypes.c_voidp)
对于 32 位,该数字为 4;对于 64 位,该数字为 8。
解决方案 5:
解释一下https://stackoverflow.com/a/1405971上的答案——
打开python控制台:
import platform
platform.architecture()[0]
它应该根据您的平台显示“64 位”或“32 位”。
或者(对于 OS X 二进制文件):
import sys
sys.maxsize > 2**32
# it should display True in case of 64bit and False in case of 32bit
解决方案 6:
在我的 Centos Linux 系统上,我执行以下操作:
1)启动 Python 解释器(我使用的是 2.6.6)
2)运行以下代码:
import platform
print(platform.architecture())
它给了我
(64bit, 'ELF')
解决方案 7:
将所有内容分组...
考虑到:
这个问题是针对OSX提出的(我有一个旧的(已破解的)虚拟机,里面有一个古老的Python版本)
我的“主要”环境是Win
我在Win上只安装了32 位( Python ) 版本(并且在Linux ( Ubuntu )上构建了一个“残缺”版本)
我将使用Python 3和Python 2在这 3 个平台上进行举例说明。
1. 检查[Python.Docs]: sys.maxsize
将值与0x100000000(4294967296或2 ** 32)进行比较:
064位更大(pc064)
032 位更小(pc032)
例子:
OSX 9 pc064:
+ *Python 2.7.10 pc064*:
> ```
> >>> import sys
> >>> "Python {:s} on {:s}".format(sys.version, sys.platform)
> 'Python 2.7.10 (default, Oct 14 2015, 05:51:29)
[GCC 4.8.2] on darwin'
> >>> hex(sys.maxsize), sys.maxsize > 0x100000000
> ('0x7fffffffffffffff', True)
>
> ```
Ubuntu 16 pc064:
+ *Python 3.5.2 pc064*:
> ```
> >>> import sys
> >>> "Python {:s} on {:s}".format(sys.version, sys.platform)
> 'Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux'
> >>> hex(sys.maxsize), sys.maxsize > 0x100000000
> ('0x7fffffffffffffff', True)
>
> ```
+ *Python 3.6.4 pc032*:
> ```
> >>> import sys
> >>> "Python {:s} on {:s}".format(sys.version, sys.platform)
> 'Python 3.6.4 (default, Apr 25 2018, 23:55:56)
[GCC 5.4.0 20160609] on linux'
> >>> hex(sys.maxsize), sys.maxsize > 0x100000000
> ('0x7fffffff', False)
>
> ```
Windows 10 pc064:
+ *Python 3.5.4 pc064*:
> ```
> >>> import sys
> >>> "Python {:s} on {: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 pc032*:
> ```
> >>> import sys
> >>> "Python {:s} on {: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)
>
> ```
2. 使用[Python.Docs]: struct.calcsize(format)
确定 (指针) 格式生成的对象大小。换句话说,确定指针大小 ( sizeof(void*)
)。
例子:
OSX 9 pc064:
+ *Python 2.7.10 pc064*:
> ```
> >>> import struct
> >>> struct.calcsize("P") * 8
> 64
>
> ```
Ubuntu 16 pc064:
+ *Python 3.5.2 pc064*:
> ```
> >>> import struct
> >>> struct.calcsize("P") * 8
> 64
>
> ```
+ *Python 3.6.4 pc032*:
> ```
> >>> import struct
> >>> struct.calcsize("P") * 8
> 32
>
> ```
Windows 10 pc064:
+ *Python 3.5.4 pc064*:
> ```
> >>> import struct
> >>> struct.calcsize("P") * 8
> 64
>
> ```
+ *Python 3.6.2 pc032*:
> ```
> >>> import struct
> >>> struct.calcsize("P") * 8
> 32
>
> ```
3. 使用[Python.Docs]: ctypes - Python 的外部函数库
它还可以归结为确定指针的大小(sizeof(void*)
)。
CTypes附注:
在某些情况下可能会使用#2.(通过[GitHub]: python/cpython - (main) cpython/Lib/ctypes/__init__.py)
检查[SO]:通过 ctypes 从 Python 调用的 C 函数返回不正确的值 (@CristiFati 的答案),以了解使用它时常见的错误(在更复杂的场景中(例如调用函数))
[SO]:Python 中 C 类型整数的最大值和最小值(@CristiFati 的答案)也可能很有趣
例子:
OSX 9 pc064:
+ *Python 2.7.10 pc064*:
> ```
> >>> import ctypes
> >>> ctypes.sizeof(ctypes.c_void_p) * 8
> 64
>
> ```
Ubuntu 16 pc064:
+ *Python 3.5.2 pc064*:
> ```
> >>> import ctypes
> >>> ctypes.sizeof(ctypes.c_void_p) * 8
> 64
>
> ```
+ *Python 3.6.4 pc032*:
> ```
> >>> import ctypes
> >>> ctypes.sizeof(ctypes.c_void_p) * 8
> 32
>
> ```
Windows 10 pc064:
+ *Python 3.5.4 pc064*:
> ```
> >>> import ctypes
> >>> ctypes.sizeof(ctypes.c_void_p) * 8
> 64
>
> ```
+ *Python 3.6.2 pc032*:
> ```
> >>> import ctypes
> >>> ctypes.sizeof(ctypes.c_void_p) * 8
> 32
>
> ```
4. 检查[Python.Docs]:platform.architecture(executable=sys.executable,bits='',linkage='')
!!! 在OSX上不可靠!!!由于多架构可执行文件(或.dylib)格式(在某些情况下,使用#2.**)。
例子:
OSX 9 pc064:
+ *Python 2.7.10 pc064*:
> ```
> >>> import platform
> >>> platform.architecture()
> ('64bit', '')
>
> ```
Ubuntu 16 pc064:
+ *Python 3.5.2 pc064*:
> ```
> >>> import platform
> >>> platform.architecture()
> ('64bit', 'ELF')
>
> ```
+ *Python 3.6.4 pc032*:
> ```
> >>> import platform
> >>> platform.architecture()
> ('32bit', 'ELF')
>
> ```
Windows 10 pc064:
+ *Python 3.5.4 pc064*:
> ```
> >>> import platform
> >>> platform.architecture()
> ('64bit', 'WindowsPE')
>
> ```
+ *Python 3.6.2 pc032*:
> ```
> >>> import platform
> >>> platform.architecture()
> ('32bit', 'WindowsPE')
>
> ```
5. 调用外部命令
蹩脚的解决方法(gainarie)
#4的限制适用(有时甚至可能不起作用):
例子:
OSX 9 pc064:
+ *Python 2.7.10 pc064*:
> ```
> >>> import os
> >>> os.system("file {:s}".format(os.path.realpath(sys.executable)))
> /opt/OPSWbuildtools/2.0.6/bin/python2.7.global: Mach-O 64-bit executable x86_64
>
> ```
Ubuntu 16 pc064:
+ *Python 3.5.2 pc064*:
> ```
> >>> import os
> >>> os.system("file {: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 pc032*:
> ```
> >>> import os
> >>> os.system("file {: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
>
> ```
Windows 10 pc064:
+ *文件*实用程序不存在,还有其他第三方工具(*Nix*模拟器:*MSYS2*、*Cygwin*等)可以使用,但我不会坚持使用它们
6.检查环境 变量
通过[Python.Docs]:os.environ:
获胜具体:
+ *%PROCESSOR_ARCHITECTURE%*(或其他)
例子:
Windows 10 pc064:
+ *Python 3.5.4 pc064*:
> ```
> >>> import os
> >>> os.environ["PROCESSOR_ARCHITECTURE"]
> 'AMD64'
>
> ```
+ *Python 3.6.2 pc032*:
> ```
> >>> import os
> >>> os.environ["PROCESSOR_ARCHITECTURE"]
> 'x86'
>
> ```
7. 检查[Python.Docs]: sys.version
启动解释器时也会显示在第一行
+ 在某些平台上,它可能包含(文本)架构信息:

检查#1。
解决方案 8:
对于非编程解决方案,请查看活动监视器。它将 64 位进程的架构列为“Intel (64 位)”。
解决方案 9:
在Windows 10上
打开 cmd 终端并通过输入 >python 启动 python 解释器,如下图所示
如果启动时的解释器信息包含AMD64,则它是 64 位,否则为 32 位。
解决方案 10:
根据最佳答案,
import sys
n_bits = 32 << bool(sys.maxsize >> 32)
n_bits 将有 32 或 64 位。
解决方案 11:
platform.architecture()
注释中说:
注意:在 Mac OS X(也许还有其他平台)上,可执行文件可能是包含多种架构的通用文件。
要获取当前解释器的“64 位性”,查询 sys.maxsize 属性更为可靠:
import sys
is_64bits = sys.maxsize > 2**32
解决方案 12:
在命令行中执行python -VV
。它应该返回版本。
解决方案 13:
struct.calcsize("P")
返回存储单个指针所需的字节大小。在 32 位系统上,它将返回 4 个字节。在 64 位系统上,它将返回 8 个字节。
32
因此,如果您运行的是 32 位 python 和64
64 位 python,则会返回以下内容:
Python 2
import struct;print struct.calcsize("P") * 8
Python 3
import struct;print(struct.calcsize("P") * 8)
解决方案 14:
C:Usersxyz>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 后
解决方案 15:
import sys
print(sys.version)
3.5.1(v3.5.1:37a07cee5969,2015 年 12 月 6 日,01:54:25)[MSC v.1900 64 位(AMD64) ]
解决方案 16:
平台架构不是可靠的方法。相反,我们:
$ 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)
解决方案 17:
platform.architecture()
是有问题的(并且昂贵)。
自 Py2.6 起可方便测试sys.maxsize > 2**32
。
这是对实际(默认)指针大小的可靠测试,并且至少从 Py2.3 开始兼容:struct.calcsize('P') == 8
。还有:ctypes.sizeof(ctypes.c_void_p) == 8
。
注意:可以使用 gcc 选项-mx32
等构建,它们是 64 位架构应用程序,但默认使用 32 位指针(节省内存和速度)。'sys.maxsize = ssize_t' 可能并不严格表示 C 指针大小(无论如何它通常2**31 - 1
是这样的)。并且有/有系统对代码和数据具有不同的指针大小,需要澄清辨别“32 位或 64 位模式”的目的究竟是什么?
解决方案 18:
使用 python 3.8 以上版本,你可以运行pyhton -VV