【问题标题】:How do I determine if my python shell is executing in 32bit or 64bit?如何确定我的 python shell 是以 32 位还是 64 位执行的?
【发布时间】:2020-06-22 18:00:53
【问题描述】:

我需要一种方法来从外壳内部判断外壳处于什么模式。

虽然我主要是 OS X 用户,但我也有兴趣了解其他平台。

我试过查看platform 模块,但它似乎只是告诉你“关于用于可执行文件的位架构和链接格式”:虽然二进制文件编译为 64 位(我正在运行OS X 10.6)所以它似乎总是报告 64 位,即使我使用方法 described here 强制 32 位模式)。

【问题讨论】:

  • 只是出于兴趣:您为什么需要知道?
  • 我在 OS X 10.6 上构建和加载某些模块时遇到问题。特别是 pysco,它抱怨我在 64 位模式下运行。这也是在 virtualenv 下,所以我需要解决一些额外的复杂问题......
  • 我想知道,因为当我使用 PyInstaller 之类的东西来创建一个独立的二进制可分发文件(给可能没有安装(正确版本的)Python 的用户时,二进制文件我生成将是 32/64 位,具体取决于我运行 PyInstaller 的 Python。理想情况下,我想我想在文件名中使用“32”或“64”自动命名生成的二进制/存档文件,而不是必须根据我执行“make”命令的位置手动重命名文件。

标签: python macos


【解决方案1】:

一种方法是查看sys.maxsize 所记录的here:

$ 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()。不幸的是,它的结果并不总是可靠的,particularly in the case of OS X universal binaries。

$ 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

【讨论】:

  • 在运行 Windows 时使用 sys.maxint 将无法检测 64 位 Python(请参阅 here)。相反,使用 struct.calcsize("P") 作为跨平台解决方案。
  • 感谢您的检查。我已经大幅修改了答案,以显示现在记录的 Python 2.6+ 的 sys.maxsize 测试和 platform 模块使用的 struct 测试,该模块也适用于旧版本的 Python 2。
  • 这在 IronPython 中不起作用,sys.maxsize 对于 32 位和 64 位 IronPython 都是 2**31
  • Yinon,这是真的,但这不是问题所要求的。而且,在那些可以运行 32 位或 64 位二进制文​​件的 CPU 上,机器的拱门通常与 Python 程序无关。重要的是 Python 解释器本身运行的架构。
  • windows cmd 需要双引号在外面,单引号在里面,否则会产生语法错误。这可能是因为 windows 认为单引号之间的空格仍然是参数分隔符。调整此答案以适应这一事实可能会有所帮助。
【解决方案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。 适用于我的特定设置。

【讨论】:

  • 所以这是什么? 64位python还是32位python?
  • @phpJs 64 位因为[MSC v.1500 64 bit (AMD64)]
  • 不幸的是,它仅适用于 Windows 版本的 Python。我的 OSX 安装返回 Python 2.7.8 (v2.7.8:ee879c0ffa11, Jun 29, 2014, 21:07:35) [GCC 4.2.1 (Apple In. build 5666) (dot 3)] on darwin
  • 在 cygwin 上,你会得到这个答案:Python 2.7.8 (default, Jul 25 2014, 14:04:36) [GCC 4.8.3] on cygwin
  • 可以通过调用sys.version在代码中找到此信息。我得到例如('3.4.4 |Continuum Analytics, Inc.| (default, Feb 16 2016, 09:54:04) [MSC ' 'v.1600 64 bit (AMD64)]') 或2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
【解决方案3】:

基本上是 Matthew Marshall 答案的一个变体(带有来自 std.library 的结构):

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

【讨论】:

  • 恕我直言,比 ctypes 版本更好 - 甚至适用于较旧的 Python。
  • 非常好用,可以一行使用。 $ python -c '导入结构体;打印 struct.calcsize("P") * 8'
  • print(struct.calcsize("P") * 8) 更好。
  • 所以复制粘贴 Python3 的 oneliner:python -c "import struct; print(struct.calcsize('P')*8)"
【解决方案4】:

尝试使用 ctypes 获取 void 指针的大小:

import ctypes
print ctypes.sizeof(ctypes.c_voidp)

32 位为 4,64 位为 8。

【讨论】:

  • 这也行得通,尽管如果您不需要 ctypes,它确实有可能存在不必要的导入和模块加载的轻微缺点:sys 模块 otoh 被编译到解释器中。
  • 请帮助我理解:在我的 64b 安装中,python -c 'import ctypes; print ctypes.sizeof(ctypes.c_voidp)' 返回 8。还是应该是python -c 'import ctypes; print ctypes.sizeof(ctypes.c_voidp) * 8'?
  • 函数返回bytes中的大小(4或8)。如果您需要bits(32 或 64)中的大小,则必须乘以 8。b_is_python_64bit = (ctypes.sizeof(ctypes.c_voidp) == 8)
  • 拨打python -c "import ctypes; print(32 if ctypes.sizeof(ctypes.c_voidp)==4 else 64, 'bit CPU')"
【解决方案5】:

打开 python 控制台:

import platform
platform.architecture()[0]

它应该根据您的平台显示“64 位”或“32 位”。

或者(in case of OS X binaries):

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')

【讨论】:

  • OP 不是将此解决方案描述为对他不起作用吗?
  • 可能是这样,但这正是我所需要的;很抱歉 +1 一个“错误的答案”,但我非常需要这个。
  • 此解决方案非常适合 Solaris 11。
【解决方案7】:

对于非编程解决方案,请查看活动监视器。它将 64 位进程的架构列为“Intel(64 位)”。

【讨论】:

  • 对于我们这些使用 Mac OS 10.x.x 的人来说,这是一个非常好的替代答案,谢谢!
【解决方案8】:

对所有内容进行分组...

考虑到:

  • 问题是针对 OSX(我有一个旧的(已破解的)VM 和一个古老的 Python 版本)
  • 我的主要环境是 Win
  • 我只在 Win 上安装了 32 位 版本(我在 Lnx 上构建了一个“残缺”版本)

我将在所有 3 个平台上举例说明,使用 Python 3 和 Python 2。

  1. 检查[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)
        
    • 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)
        
    • Win 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)
        


  1. 使用[Python 3.Docs]: struct.calcsize(format) 确定(指针)格式产生的对象大小。换句话说,确定指针大小(sizeof(void*)):
    • OSX 9 x64:
      • Python 2.7.10 x64:
        >>> import struct
        >>> struct.calcsize("P") * 8
        64
        
    • 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
        
    • Win 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
        


  1. 使用[Python 3.Docs]: ctypes - A foreign function library for Python。它还归结为确定指针的大小 (sizeof(void*))。请注意,ctypes 通过 "${PYTHON_SRC_DIR}/Lib/ctypes 使用 #2.(不一定用于此任务) /__init__.py"(围绕行#15):
    • OSX 9 x64:
      • Python 2.7.10 x64:
        >>> import ctypes
        >>> ctypes.sizeof(ctypes.c_void_p) * 8
        64
        
    • 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
        
    • Win 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
        


  1. [Python 3.Docs]: platform.architecture(executable=sys.executable, bits='', linkage='')!!!在 OSX 上不可靠!!! 由于多架构可执行文件(或 .dylib)格式(在某些情况下,使用 #2 .):
    • OSX 9 x64:
      • Python 2.7.10 x64:
        >>> import platform
        >>> platform.architecture()
        ('64bit', '')
        
    • 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')
        
    • Win 10 x64:
      • Python 3.5.4 x64:
        >>> import platform
        >>> platform.architecture()
        ('64bit', 'WindowsPE')
        
      • Python 3.6.2 x86:
        >>> import platform
        >>> platform.architecture()
        ('32bit', 'WindowsPE')
        


  1. 蹩脚的解决方法 (gainarie) - 通过 [Python 3.Docs]: os.system(command) 调用外部命令 ([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
        
    • 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
        
    • Win 10 x64:
      • file 实用程序不存在,还有其他 3rd 派对工具可以使用,但我不会坚持使用它们


赢具体:

  1. 通过[Python 3.Docs]: os.environ检查环境变量(例如%PROCESSOR_ARCHITECTURE%(或其他)):
    • Win 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'
        


  1. [Python 3.Docs]: sys.version(也在启动解释器时显示在第 1st 行)
    • 检查 #1.

【讨论】:

    【解决方案9】:

    对于 32 位,它将返回 32,对于 64 位,它将返回 64

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

    【讨论】:

      【解决方案10】:

      platform.architecture() 笔记说:

      注意: 在 Mac OS X(可能还有其他平台)上,可执行文件可能是包含多种架构的通用文件。

      获取当前解释器的“64位”,更可靠 查询 sys.maxsize 属性:

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

      【讨论】:

        【解决方案11】:

        在命令行中执行python -VV。它应该返回版本。

        【讨论】:

        • 这似乎没有提供回答问题所需的信息
        • Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] 谢谢
        • 在我的情况下也没有提供:python3.6 -VV: Python 3.6.9 (default, Jul 17 2020, 12:50:27) [GCC 8.4.0]
        • 我只有Python 2.7.16
        【解决方案12】:

        在 Windows 10

        上

        打开 cmd 终端并通过键入 >python 启动 python 解释器,如下图所示

        如果开始的解释器信息包含 AMD64,则为 64 位,否则为 32 位。

        【讨论】:

        • python 二进制文件的许多常见发行版在启动时不会打印此详细语句
        【解决方案13】:

        struct.calcsize("P") 返回存储单个指针所需的字节大小。在 32 位系统上,它将返回 4 个字节。在 64 位系统上,它将返回 8 个字节。

        因此,如果您正在运行 32 位 python,则以下内容将返回 32,如果您正在运行 64 位 python,则返回 64:

        Python 2

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

        Python 3

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

        【讨论】:

          【解决方案14】:

          试试这个:

          import platform
          platform.architecture()
          

          【讨论】:

            【解决方案15】:

            基于abe32's answer,

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

            n_bits 将有 32 或 64 位。

            【讨论】:

              【解决方案16】:
              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后

              【讨论】:

              • 那么是版本吗? 64 还是 32?
              【解决方案17】:
              import sys
              print(sys.version)
              

              3.5.1(v3.5.1:37a07cee5969,2015 年 12 月 6 日,01:54:25)[MSC v.1900 64 位 (AMD64)]

              【讨论】:

              • 不适用于 OSX。
              【解决方案18】:

              平台架构不是可靠的方法。 取而代之的是我们:

              $ 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)
              

              【讨论】:

                【解决方案19】:

                platform.architecture() 有问题(而且很昂贵)。

                从 Py2.6 开始方便地测试 sys.maxsize &gt; 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位模式”的确切目的是什么?

                【讨论】:

                  猜你喜欢
                  • 2010-10-23
                  • 2011-04-04
                  • 2010-09-19
                  • 2010-12-28
                  • 2011-04-19
                  • 2012-03-02
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多