且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-09 19:22:34

对于2.6和更高版本的Python,您可以按照

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