简体   繁体   English

如何确定我的 python shell 是在 32 位还是 64 位中执行?

[英]How do I determine if my python shell is executing in 32bit or 64bit?

I need a way to tell what mode the shell is in from within the shell.我需要一种方法来从 shell 内部判断 shell 处于什么模式。

While I'm primarily an OS X user, I'd be interested in knowing about other platforms as well.虽然我主要是 OS X 用户,但我也有兴趣了解其他平台。

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 上运行)所以即使我使用此处描述的方法强制使用 32 位模式,它似乎总是报告 64 位)。

One way is to look at sys.maxsize as documented here :一种方法是查看此处记录的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 was introduced in Python 2.6. sys.maxsize是在 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:如果您需要对旧系统进行测试,这个稍微复杂的测试应该适用于所有 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.顺便说一句,您可能会为此使用platform.architecture() Unfortunately, its results are not always reliable, particularly in the case of OS X universal binaries .不幸的是,它的结果并不总是可靠的, 特别是在 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

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

Where [MSC v.1500 64 bit (AMD64)] means 64-bit Python.其中[MSC v.1500 64 bit (AMD64)]表示 64 位 Python。 Works for my particular setup.适用于我的特定设置。

Basically a variant on Matthew Marshall's answer (with struct from the std.library):基本上是 Matthew Marshall 答案的一个变体(带有来自 std.library 的结构):

import struct
print struct.calcsize("P") * 8

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. 32 位为 4,64 位为 8。

Open python console:打开python控制台:

import platform
platform.architecture()[0]

it should display the '64bit' or '32bit' according to your platform.它应该根据您的平台显示“64bit”或“32bit”。

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

On my Centos Linux system I did the following:在我的 Centos Linux 系统上,我执行了以下操作:

1) Started the Python interpreter (I'm using 2.6.6) 1) 启动 Python 解释器(我使用的是 2.6.6)
2) Ran the following code: 2)运行以下代码:

import platform
print(platform.architecture())

and it gave me它给了我

(64bit, 'ELF')

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 位)”。

Grouping everything...将所有内容分组...

Considering that:考虑到:

  • The question is asked for OSX (I have an old (and cracked) VM with an ancient Python version)这个问题是针对OSX提出的(我有一个带有古老Python版本的旧(和破解) VM
  • My main env is Win我的主要环境是Win
  • I only have the 32bit version installed on Win (and I built a "crippled" one on Lnx )我只在Win 上安装了32 位版本(并且我在Lnx上构建了一个“残废”版本

I'm going to exemplify on all 3 platforms, using Python 3 and Python 2 .我将在所有 3 个平台上举例说明,使用Python 3Python 2

  1. Check [Python 3.Docs]: sys.检查[Python 3.Docs]: sys. maxsize value - compare it to 0x100000000 ( 2 ** 32 ): greater for 64bit , smaller for 32bit : MAXSIZE值-它比较0x1000000002 ** 32 ):大于64位,32位为较小:
    • OSX 9 x64 : OSX 9 x64
      • Python 2.7.10 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)
    • Ubuntu 16 x64 : Ubuntu 16 x64
      • Python 3.5.2 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 : 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)
    • Win 10 x64 :赢得 10 x64
      • Python 3.5.4 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 : 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)


  1. Use [Python 3.Docs]: struct.使用[Python 3.Docs]: struct。 calcsize ( format ) to determine the object size produced by the (pointer) format. calcsize ( format )来确定由 (pointer) format 产生的对象大小。 In other words, determines the pointer size ( sizeof(void*) ):换句话说,确定指针大小( sizeof(void*) ):
    • OSX 9 x64 : OSX 9 x64
      • Python 2.7.10 x64 : Python 2.7.10 x64
         >>> import struct >>> struct.calcsize("P") * 8 64
    • Ubuntu 16 x64 : Ubuntu 16 x64
      • Python 3.5.2 x64 : Python 3.5.2 x64
         >>> import struct >>> struct.calcsize("P") * 8 64
      • Python 3.6.4 x86 : Python 3.6.4 x86
         >>> import struct >>> struct.calcsize("P") * 8 32
    • Win 10 x64 :赢得 10 x64
      • Python 3.5.4 x64 : Python 3.5.4 x64
         >>> import struct >>> struct.calcsize("P") * 8 64
      • Python 3.6.2 x86 : Python 3.6.2 x86
         >>> import struct >>> struct.calcsize("P") * 8 32


  1. Use [Python 3.Docs]: ctypes - A foreign function library for Python .使用[Python 3.Docs]: ctypes - Python 的外部函数库 It also boils down to determining the size of a pointer ( sizeof(void*) ).它也归结为确定指针的大小( sizeof(void*) )。 As a note, ctypes uses #2.注意, ctypes使用#2。 (not necessarily for this task) via "${PYTHON_SRC_DIR}/Lib/ctypes/__init__.py" (around line #15 ): (不一定针对此任务)通过“${PYTHON_SRC_DIR}/Lib/ctypes/__init__.py” (围绕#15):
    • OSX 9 x64 : OSX 9 x64
      • Python 2.7.10 x64 : Python 2.7.10 x64
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
    • Ubuntu 16 x64 : Ubuntu 16 x64
      • Python 3.5.2 x64 : Python 3.5.2 x64
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
      • Python 3.6.4 x86 : Python 3.6.4 x86
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32
    • Win 10 x64 :赢得 10 x64
      • Python 3.5.4 x64 : Python 3.5.4 x64
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
      • Python 3.6.2 x86 : Python 3.6.2 x86
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32


  1. [Python 3.Docs]: platform. [Python 3.Docs]:平台。 architecture ( executable=sys.executable, bits='', linkage='' ) !!! 架构executable=sys.executable, bits='', links='' !!! NOT reliable on OSX !!!OSX上不可靠!!! due to multi arch executable (or .dylib ) format (in some cases, uses #2. ):由于 multi arch 可执行文件(或.dylib )格式(在某些情况下,使用#2. ):
    • OSX 9 x64 : OSX 9 x64
      • Python 2.7.10 x64 : Python 2.7.10 x64
         >>> import platform >>> platform.architecture() ('64bit', '')
    • Ubuntu 16 x64 : Ubuntu 16 x64
      • Python 3.5.2 x64 : Python 3.5.2 x64
         >>> import platform >>> platform.architecture() ('64bit', 'ELF')
      • Python 3.6.4 x86 : Python 3.6.4 x86
         >>> import platform >>> platform.architecture() ('32bit', 'ELF')
    • Win 10 x64 :赢得 10 x64
      • Python 3.5.4 x64 : Python 3.5.4 x64
         >>> import platform >>> platform.architecture() ('64bit', 'WindowsPE')
      • Python 3.6.2 x86 : Python 3.6.2 x86
         >>> import platform >>> platform.architecture() ('32bit', 'WindowsPE')


  1. Lame workaround ( gainarie ) - invoke an external command ( [man7]: FILE(1) ) via [Python 3.Docs]: os.蹩脚的解决方法 ( Gainarie ) - 通过[Python 3.Docs]调用外部命令 ( [man7]: FILE(1) ) : os. system ( command ) . 系统命令 The limitations of #4. #4的局限性 apply (sometimes it might not even work):应用(有时它甚至可能不起作用):
    • OSX 9 x64 : OSX 9 x64
      • Python 2.7.10 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
    • Ubuntu 16 x64 : Ubuntu 16 x64
      • Python 3.5.2 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 : 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
    • Win 10 x64 :赢得 10 x64
      • file utility is not present, there are other 3 rd Party tools that can be used, but I'm not going to insist on them文件实用程序不存在,还有其他 3 rd Party 工具可以使用,但我不会坚持使用它们


Win specific:具体:

  1. Check env vars (eg %PROCESSOR_ARCHITECTURE% (or others)) via [Python 3.Docs]: os.通过[Python 3.Docs]检查环境变量(例如%PROCESSOR_ARCHITECTURE% (或其他)) environ :环境
    • Win 10 x64 :赢得 10 x64
      • Python 3.5.4 x64 : Python 3.5.4 x64
         >>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'AMD64'
      • Python 3.6.2 x86 : Python 3.6.2 x86
         >>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'x86'


  1. [Python 3.Docs]: sys. [Python 3.Docs]: sys. version (also displayed in the 1 st line when starting the interpreter) 版本(启动解释时也显示在第一
    • Check #1.检查#1。

For 32 bit it will return 32 and for 64 bit it will return 64对于 32 位,它将返回 32,对于 64 位,它将返回 64

import struct
print(struct.calcsize("P") * 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.maxsize 属性更可靠:

import sys
is_64bits = sys.maxsize > 2**32

Do a python -VV in the command line.在命令行中执行python -VV It should return the version.它应该返回版本。

On Windows 10Windows 10 上

Open the cmd termial and start python interpreter by typing >python as shown in the below image打开 cmd 终端并通过键入 >python 启动 python 解释器,如下图所示

图像

If the interpreter info at start contains AMD64 , it's 64-bit, otherwise, 32-bit bit.如果开始时的解释器信息包含AMD64则为64 位,否则为 32 位。

struct.calcsize("P") returns size of the bytes required to store a single pointer. struct.calcsize("P")返回存储单个指针所需的字节大小。 On a 32-bit system, it would return 4 bytes.在 32 位系统上,它将返回 4 个字节。 On a 64-bit system, it would return 8 bytes.在 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 ,如果你正在运行的32位Python和64 ,如果你正在运行的64位蟒蛇:

Python 2蟒蛇 2

import struct;print struct.calcsize("P") * 8

Python 3蟒蛇 3

import struct;print(struct.calcsize("P") * 8)

Based On abe32's answer,基于abe32 的回答,

import sys
n_bits = 32 << bool(sys.maxsize >> 32)

n_bits will have 32 or 64 bits. n_bits 将有 32 位或 64 位。

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 后

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 (v3.5.1:37a07cee5969, 2015 年 12 月 6 日,01:54:25) [MSC v.1900 64 位 (AMD64) ]

Try this:试试这个:

import platform
platform.architecture()

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)

platform.architecture() is problematic (and expensive). platform.architecture()是有问题的(而且很昂贵)。

Conveniently test for sys.maxsize > 2**32 since Py2.6 .自 Py2.6 以来,方便地测试sys.maxsize > 2**32

This is a reliable test for the actual (default) pointer size and compatible at least since Py2.3: struct.calcsize('P') == 8 .这是对实际(默认)指针大小的可靠测试,至少自 Py2.3 起兼容: struct.calcsize('P') == 8 Also: ctypes.sizeof(ctypes.c_void_p) == 8 .另外: ctypes.sizeof(ctypes.c_void_p) == 8

Notes: There can be builds with gcc option -mx32 or so, which are 64bit architecture applications, but use 32bit pointers as default (saving memory and speed).注意:可以使用 gcc 选项-mx32左右构建,它们是 64 位架构应用程序,但默认使用 32 位指针(节省内存和速度)。 'sys.maxsize = ssize_t' may not strictly represent the C pointer size (its usually 2**31 - 1 anyway). 'sys.maxsize = ssize_t' 可能不严格代表 C 指针大小(它通常是2**31 - 1反正)。 And there were/are systems which have different pointer sizes for code and data and it needs to be clarified what exactly is the purpose of discerning "32bit or 64bit mode?"并且有些系统对代码和数据具有不同的指针大小,需要澄清辨别“32 位或 64 位模式”的目的究竟是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM