哪个命令用于检查python是64位还是32位 [英] Which command to use for checking whether python is 64bit or 32bit

查看:36
本文介绍了哪个命令用于检查python是64位还是32位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何命令来检查我的 python 是为 32 位系统还是 64 位系统编译的.

I am not able to find any command to check if my python is compiled for 32bit system or 64bit system.

我试过了

蟒蛇

它只告诉版本

此外,当我访问 python 下载站点时,他们有一个适用于 linux 的 python 版本,但有两个适用于 mac 的版本,即 32 位和 64 位.

Also when I go to python download site they have one version of python for linux but two versions for mac i.e 32bit and 64bit.

推荐答案

对于 Python 2.6 及更高版本,您可以使用 sys.maxsize 作为文档这里:

For Python 2.6 and above, you can use sys.maxsize as documented here:

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

更新:我注意到我并没有真正回答提出的问题.虽然上面的测试确实准确地告诉你解释器是在 32 位还是 64 位架构中运行,但它没有也不能回答这个解释器是为什么构建的完整架构集的问题并且可以运行.正如问题中所指出的,这很重要,例如对于 Mac OS X 通用可执行文件,其中一个可执行文件可能包含多个体系结构的代码.回答这个问题的一种方法是使用操作系统 file 命令.在大多数系统上,它会报告可执行文件的受支持架构.以下是在大多数系统上通过 shell 命令行在一行中执行此操作的方法:

UPDATE: I notice that I didn't really answer the question posed. While the above test does accurately tell you whether the interpreter is running in a 32-bit or a 64-bit architecture, it doesn't and can't answer the question of what is the complete set of architectures that this interpreter was built for and could run in. As was noted in the question, this is important for example with Mac OS X universal executables where one executable file may contain code for multiple architectures. One way to answer that question is to use the operating system file command. On most systems it will report the supported architectures of an executable file. Here's how to do it in one line from a shell command line on most systems:

file -L $(python -c 'import sys; print(sys.executable)')

在 OS X 10.6 上使用默认系统 Python,输出为:

Using the default system Python on OS X 10.6, the output is:

/usr/bin/python: Mach-O universal binary with 3 architectures
/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64
/usr/bin/python (for architecture i386):    Mach-O executable i386
/usr/bin/python (for architecture ppc7400): Mach-O executable ppc

在一个 Linux 系统上:

On one Linux system:

/usr/bin/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, stripped

顺便说一句,这里有一个例子,说明为什么 platform 为此目的不可靠.再次在 OS X 10.6 上使用系统 Python:

BTW, here's an example of why platform is not reliable for this purpose. Again using the system Python on OS X 10.6:

$ 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

这篇关于哪个命令用于检查python是64位还是32位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆