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

发布于 2024-11-09 12:34:09 字数 190 浏览 2 评论 0 原文

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

我试过

蟒蛇

,它只告诉版本

另外,当我去python下载网站时,他们有一个用于linux的python版本,但有两个用于mac的版本,即32位和64位。

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

I tried

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 i.e 32bit and 64bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

动次打次papapa 2024-11-16 12:34:09

对于 Python 2.6 及更高版本,您可以使用 此处

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

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

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

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

/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 系统上:

/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:

$ 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

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

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

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

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

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
终难遇 2024-11-16 12:34:09
import platform
platform.architecture()[0]
#'32bit'
import platform
platform.architecture()[0]
#'32bit'
邮友 2024-11-16 12:34:09

首先,打开cmd并输入

$ python

然后,输入以下两行

>>> import platform

>>> platform.architecture()

First, open cmd and type in

$ python

Then, type in the following two lines

>>> import platform

>>> platform.architecture()
猫卆 2024-11-16 12:34:09

在 Linux 控制台中输入:

  1. 如果您想使用应用程序的运行命令来检查应用程序是具有 64 位还是 32 位架构:
type -p <command_to_run_application> | xargs readlink -f | xargs file -b | sed 's/, /\n/g' | sed -n 2p
  • 如果您想通过使用应用程序的完整路径来检查应用程序是具有 64 位还是 32 位架构:
  • file -b <full_path_to_an_application> | sed 's/, /\n/g' | sed -n 2p
    

    例如,对于 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
    

    可能的输出:

    x86-64
    

    or

    Intel 80386
    

    or

    ARM
    

    or other。

    如果输出是“Intel 80386”,则表明应用程序具有 32 位架构。

    如果输出为“x86-64”,则表明应用程序具有 64 位架构。

    Type in Linux console:

    1. In case when you want check whether an application has 64 bit or 32 bit architecture by using its command for run:
    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:
    file -b <full_path_to_an_application> | sed 's/, /\n/g' | sed -n 2p
    

    For example, for Python 3 corresponding commands can be:

    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:

    x86-64
    

    or

    Intel 80386
    

    or

    ARM
    

    or other.

    If output is "Intel 80386" than the application has 32 bit architecture.

    If output is "x86-64" than the application has 64 bit architecture.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文