Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何确定python shell是以32位还是64位执行?_Python_Macos - Fatal编程技术网

如何确定python shell是以32位还是64位执行?

如何确定python shell是以32位还是64位执行?,python,macos,Python,Macos,我需要一种从shell内部判断shell处于何种模式的方法 虽然我主要是一个OSX用户,但我也有兴趣了解其他平台 我试着看一下这个模块,但它似乎只告诉你“关于位体系结构和用于可执行文件的链接格式”:二进制文件被编译为64位(我在OS X 10.6上运行)因此,即使我使用强制32位模式的方法,它似乎总是报告64位。一种方法是查看sys.maxsize,如文档所示: sys.maxsize是在Python2.6中引入的。如果您需要对较旧的系统进行测试,这个稍微复杂一些的测试应该适用于所有Python

我需要一种从shell内部判断shell处于何种模式的方法

虽然我主要是一个OSX用户,但我也有兴趣了解其他平台


我试着看一下这个模块,但它似乎只告诉你“关于位体系结构和用于可执行文件的链接格式”:二进制文件被编译为64位(我在OS X 10.6上运行)因此,即使我使用强制32位模式的方法,它似乎总是报告64位。

一种方法是查看
sys.maxsize
,如文档所示:

sys.maxsize
是在Python2.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()
。不幸的是,它的结果并不总是可靠的


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

import ctypes
print ctypes.sizeof(ctypes.c_voidp)

32位为4,64位为8。

基本上是Matthew Marshall答案的一个变体(使用std.library中的struct):


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

在终端/命令行中启动Python解释器时,您可能还会看到一行,如:

win32上的Python 2.7.2(默认值,2011年6月12日14:24:46)[MSC v.1500 64位(AMD64)]

其中
[MSC v.1500 64位(AMD64)]
表示64位Python。 适用于我的特定设置

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.
>>>

在Centos Linux系统上,在cmd中点击python后,我做了以下操作:

1) 启动Python解释器(我使用的是2.6.6)
2) 运行以下代码:

import platform
print(platform.architecture())
它给了我

(64bit, 'ELF')
便笺上写着:

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

要获得当前解释器的“64位”,它更可靠 要查询sys.maxsize属性,请执行以下操作:


打开python控制台:

import platform
platform.architecture()[0]
它应该根据您的平台显示“64位”或“32位”

或者():

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)

3.5.1(v3.5.1:37A07CEE59692015年12月6日01:54:25)[MSC v.190064位(AMD64)]

平台架构不是可靠的方式。 相反,我们:

$ 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)
将所有内容分组

考虑到:

  • 这个问题是针对OSX提出的(我有一个旧的(已经破解的)VM,它有一个古老的Python版本)
  • 我的主要任务是赢
  • 我在Win上只安装了32位版本(我在Lnx上构建了一个“残废”版本)
我将使用Python3和Python2在所有3个平台上举例说明

  • 检查值-将其与
    0x100000000
    2**32
    )进行比较:64位较大,32位较小:
    • OSX 9 x64:
      • Python 2.7.10 x64:
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
      • Python 3.6.4 x86:
    • Win 10 x64:
      • Python 3.5.4 x64:
      • Python 3.6.2 x86:

  • 用于确定(指针)格式生成的对象大小。换句话说,确定指针大小(
    sizeof(void*)
    ):
    • OSX 9 x64:
      • Python 2.7.10 x64:
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
      • Python 3.6.4 x86:
    • Win 10 x64:
      • Python 3.5.4 x64:
      • Python 3.6.2 x86:

  • 使用。它还可以归结为确定指针的大小(
    sizeof(void*)
    )。注意,ctypes通过“${PYTHON\u SRC\u DIR}/Lib/ctypes/uuu init\uuuuuuuuuuy.py”(第15行附近)使用#2.(不一定用于此任务):
    • OSX 9 x64:
      • Python 2.7.10 x64:
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
      • Python 3.6.4 x86:
    • Win 10 x64:
      • Python 3.5.4 x64:
      • Python 3.6.2 x86:

  • !!!在OSX上不可靠由于多拱可执行文件(或.dylib)格式(在某些情况下,使用#2.):
    • OSX 9 x64:
      • Python 2.7.10 x64:
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
      • Python 3.6.4 x86:
    • Win 10 x64:
      • Python 3.5.4 x64:
      • Python 3.6.2 x86:

  • Lame变通方法(gainarie)-通过调用外部命令()。#4.的局限性适用(有时甚至可能不起作用):
    • OSX 9 x64:
      • Python 2.7.10 x64:
    • Ubuntu 16 x64:
      • Python 3.5.2 x64:
      • Python 3.6.4 x86:
    • Win 10 x64:
      • 文件工具不存在,还有其他第三方工具可以使用,但我不会坚持使用它们

  • Win-specific:
  • 通过以下方式检查环境变量(例如%PROCESSOR_ARCHITECTURE%(或其他)):
    • Win 10 x64:
      • Python 3.5.4 x64:
      • Python 3.6.2 x86:

  • (启动口译员时,第1行也显示
    • 选中#1.

  • platform.architecture()

    方便地测试自Py2.6以来的
    sys.maxsize>2**32

    这是对实际(默认)指针大小的可靠测试,至少从Py2.3开始兼容:import platform platform.architecture()[0]
    import sys
    sys.maxsize > 2**32 
    # it should display True in case of 64bit and False in case of 32bit
    
    import struct;print struct.calcsize("P") * 8
    
    import struct;print(struct.calcsize("P") * 8)
    
    import sys
    print(sys.version)
    
    $ 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)
    
    >>> 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)
    
    >>> 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)
    
    >>> 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)
    
    >>> 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)
    
    >>> 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)
    
    >>> import struct
    >>> struct.calcsize("P") * 8
    64
    
    >>> import struct
    >>> struct.calcsize("P") * 8
    64
    
    >>> import struct
    >>> struct.calcsize("P") * 8
    32
    
    >>> import struct
    >>> struct.calcsize("P") * 8
    64
    
    >>> import struct
    >>> struct.calcsize("P") * 8
    32
    
    >>> import ctypes
    >>> ctypes.sizeof(ctypes.c_void_p) * 8
    64
    
    >>> import ctypes
    >>> ctypes.sizeof(ctypes.c_void_p) * 8
    64
    
    >>> import ctypes
    >>> ctypes.sizeof(ctypes.c_void_p) * 8
    32
    
    >>> import ctypes
    >>> ctypes.sizeof(ctypes.c_void_p) * 8
    64
    
    >>> import ctypes
    >>> ctypes.sizeof(ctypes.c_void_p) * 8
    32
    
    >>> import platform
    >>> platform.architecture()
    ('64bit', '')
    
    >>> import platform
    >>> platform.architecture()
    ('64bit', 'ELF')
    
    >>> import platform
    >>> platform.architecture()
    ('32bit', 'ELF')
    
    >>> import platform
    >>> platform.architecture()
    ('64bit', 'WindowsPE')
    
    >>> import platform
    >>> platform.architecture()
    ('32bit', 'WindowsPE')
    
    >>> 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
    
    >>> 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
    
    >>> 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
    
    >>> import os
    >>> os.environ["PROCESSOR_ARCHITECTURE"]
    'AMD64'
    
    >>> import os
    >>> os.environ["PROCESSOR_ARCHITECTURE"]
    'x86'
    
    import sys
    n_bits = 32 << bool(sys.maxsize >> 32)
    
    import struct
    print(struct.calcsize("P") * 8)