如何检测 Python 是否作为 64 位应用程序运行?

发布于 2024-08-13 19:53:45 字数 158 浏览 1 评论 0原文

我正在对 Windows 注册表进行一些工作。根据 Python 运行为 32 位还是 64 位,某些键值会有所不同。如何检测 Python 是作为 64 位应用程序运行还是作为 32 位应用程序运行? (我对检测 32 位/64 位 Windows 不感兴趣 - 只是对 Python 平台感兴趣。)

I'm doing some work with the Windows registry. Depending on whether Python is running as 32-bit or 64-bit, certain key values will be different. How can I detect whether Python is running as a 64-bit application or as a 32-bit application? (I'm not interested in detecting 32-bit/64-bit Windows - just the Python platform.)

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

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

发布评论

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

评论(2

孤蝉 2024-08-20 19:53:45
import platform
platform.architecture()

来自 Python 文档

查询给定的可执行文件(默认
到 Python 解释器二进制文件)
各种架构信息。

返回一个元组(位,链接),其中
包含有关位的信息
架构和链接格式
用于可执行文件。两个值
以字符串形式返回。

import platform
platform.architecture()

From the Python docs:

Queries the given executable (defaults
to the Python interpreter binary) for
various architecture information.

Returns a tuple (bits, linkage) which
contain information about the bit
architecture and the linkage format
used for the executable. Both values
are returned as strings.

南七夏 2024-08-20 19:53:45

虽然它可能适用于某些平台,但请注意,platform.architecture 并不总是确定 python 是在 32 位还是 64 位运行的可靠方法。特别是,在某些 OS X 多架构构建上,相同的可执行文件可能能够在任一模式下运行,如下面的示例所示。最快的安全多平台方法是在 Python 2.6、2.7、Python 3.x 上测试 sys.maxsize

$ 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)

While it may work on some platforms, be aware that platform.architecture is not always a reliable way to determine whether python is running in 32-bit or 64-bit. In particular, on some OS X multi-architecture builds, the same executable file may be capable of running in either mode, as the example below demonstrates. The quickest safe multi-platform approach is to test sys.maxsize on Python 2.6, 2.7, Python 3.x.

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