简体   繁体   English

哪个命令用于检查 python 是 64 位还是 32 位

[英]Which command to use for checking whether python is 64bit or 32bit

I am not able to find any command to check if my python is compiled for 32bit system or 64bit system.我找不到任何命令来检查我的 python 是为 32 位系统还是 64 位系统编译的。

I tried我试过了

python python

and it only tells the version它只告诉版本

Also when I go to python download site they have one version of python for linux but two versions for mac ie 32bit and 64bit.此外,当我从 go 到 python 下载站点时,他们有一个版本的 python 用于 linux,但有两个版本用于 mac,即 32 位和 64 位。

For Python 2.6 and above, you can use sys.maxsize as documented here :对于 Python 2.6 及更高版本,您可以使用此处记录的sys.maxsize

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

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.虽然上面的测试确实准确地告诉您解释器是在 32 位还是 64 位架构中运行,但它没有也不能回答这个解释器构建的完整架构集是什么的问题并且可以运行。正如问题中所述,这对于例如 Mac OS X 通用可执行文件很重要,其中一个可执行文件可能包含多个体系结构的代码。 One way to answer that question is to use the operating system file command.回答这个问题的一种方法是使用操作系统file命令。 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:以下是在大多数系统上通过 shell 命令行在一行中执行此操作的方法:

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

Using the default system Python on OS X 10.6, the output is:在 OS X 10.6 上使用默认系统 Python,output 是:

/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

On one Linux system:在一个 Linux 系统上:

/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

BTW, here's an example of why platform is not reliable for this purpose.顺便说一句,这里有一个例子说明为什么platform对此不可靠。 Again using the system Python on OS X 10.6:再次在 OS X 10.6 上使用系统 Python:

$ 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
import platform
platform.architecture()[0]
#'32bit'

First, open cmd and type in首先,打开 cmd 并输入

$ python

Then, type in the following two lines然后,输入以下两行

>>> import platform

>>> platform.architecture()

Type in Linux console:输入 Linux 控制台:

  1. In case when you want check whether an application has 64 bit or 32 bit architecture by using its command for run:如果您想通过运行命令检查应用程序是 64 位还是 32 位架构:
type -p <command_to_run_application> | xargs readlink -f | xargs file -b | sed 's/, /\n/g' | sed -n 2p
  1. In case when you want check whether an application has 64 bit or 32 bit architecture by using full path to the application:如果您想使用应用程序的完整路径检查应用程序是 64 位还是 32 位架构:
file -b <full_path_to_an_application> | sed 's/, /\n/g' | sed -n 2p

For example, for Python 3 corresponding commands can be:例如对于Python 3对应的命令可以是:

type -p python3 | xargs readlink -f | xargs file -b | sed 's/, /\n/g' | sed -n 2p
file -b /usr/bin/python3.5 | sed 's/, /\n/g' | sed -n 2p

Possible output:可能的 output:

x86-64

or或者

Intel 80386

or或者

ARM

or other.或其他。

If output is "Intel 80386" than the application has 32 bit architecture.如果 output 是“Intel 80386”,则应用程序具有 32 位架构。

If output is "x86-64" than the application has 64 bit architecture.如果 output 是“x86-64”,则应用程序具有 64 位架构。

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

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