如何使用 Apple Silicon 获得在 MacOS 上运行 Python 解释器的架构?

发布于 2025-01-15 15:52:30 字数 275 浏览 3 评论 0原文

运行Python通用二进制文件(x86_64、arm64)时,如何从Python内部检查它是否正在运行x86_64或arm64可执行文件?似乎 os.uname() 和 platform.machine() 总是给出实际的系统架构(即“arm64”),而 sysconfig.get_platform( ) 总是会给出“universal2”(也没有帮助)。

那么如何区分 Python 作为 python3arch -x86_64 python3 运行的区别呢?

When running a Python universal binary (x86_64, arm64), how can I check from within Python whether it is running the x86_64 or arm64 executable? It seems that os.uname() and platform.machine() would always give the actual system architecture (i.e. "arm64"), and sysconfig.get_platform() would always give "universal2" (also not helping).

So how to tell the difference between Python being run as python3 or arch -x86_64 python3?

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

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

发布评论

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

评论(2

浅忆 2025-01-22 15:52:30

好的,我认为以下函数应该报告正确的机器体系结构(即正在运行的解释器的机器体系结构,而不是系统的体系结构),也适用于其他操作系统:

def get_platform():
"""Return a string with current platform (system and machine architecture).

This attempts to improve upon `sysconfig.get_platform` by fixing some
issues when running a Python interpreter with a different architecture than
that of the system (e.g. 32bit on 64bit system, or a multiarch build),
which should return the machine architecture of the currently running
interpreter rather than that of the system (which didn't seem to work
properly). The reported machine architectures follow platform-specific
naming conventions (e.g. "x86_64" on Linux, but "x64" on Windows).

Example output strings for common platforms:

    darwin_(ppc|ppc64|i368|x86_64|arm64)
    linux_(i686|x86_64|armv7l|aarch64)
    windows_(x86|x64|arm32|arm64)

"""

system = platform.system().lower()
machine = sysconfig.get_platform().split("-")[-1].lower()
is_64bit = sys.maxsize > 2 ** 32

if system == "darwin": # get machine architecture of multiarch binaries
    if any([x in machine for x in ("fat", "intel", "universal")]):
        machine = platform.machine().lower()

elif system == "linux":  # fix running 32bit interpreter on 64bit system
    if not is_64bit and machine == "x86_64":
        machine = "i686"
    elif not is_64bit and machine == "aarch64":
            machine = "armv7l"

elif system == "windows": # return more precise machine architecture names
    if machine == "amd64":
        machine = "x64"
    elif machine == "win32":
        if is_64bit:
            machine = platform.machine().lower()
        else:
            machine = "x86"

# some more fixes based on examples in https://en.wikipedia.org/wiki/Uname
if not is_64bit and machine in ("x86_64", "amd64"):
    if any([x in system for x in ("cygwin", "mingw", "msys")]):
        machine = "i686"
    else:
        machine = "i386"

return f"{system}_{machine}"

不幸的是,我无法访问 M1 Mac 进行双重检查。如果有人能确认这有效,那就太好了。

Okay, I think the following function should report the correct machine architectures (i.e. that of the running interpreter, not that of the system), also for other OS:

def get_platform():
"""Return a string with current platform (system and machine architecture).

This attempts to improve upon `sysconfig.get_platform` by fixing some
issues when running a Python interpreter with a different architecture than
that of the system (e.g. 32bit on 64bit system, or a multiarch build),
which should return the machine architecture of the currently running
interpreter rather than that of the system (which didn't seem to work
properly). The reported machine architectures follow platform-specific
naming conventions (e.g. "x86_64" on Linux, but "x64" on Windows).

Example output strings for common platforms:

    darwin_(ppc|ppc64|i368|x86_64|arm64)
    linux_(i686|x86_64|armv7l|aarch64)
    windows_(x86|x64|arm32|arm64)

"""

system = platform.system().lower()
machine = sysconfig.get_platform().split("-")[-1].lower()
is_64bit = sys.maxsize > 2 ** 32

if system == "darwin": # get machine architecture of multiarch binaries
    if any([x in machine for x in ("fat", "intel", "universal")]):
        machine = platform.machine().lower()

elif system == "linux":  # fix running 32bit interpreter on 64bit system
    if not is_64bit and machine == "x86_64":
        machine = "i686"
    elif not is_64bit and machine == "aarch64":
            machine = "armv7l"

elif system == "windows": # return more precise machine architecture names
    if machine == "amd64":
        machine = "x64"
    elif machine == "win32":
        if is_64bit:
            machine = platform.machine().lower()
        else:
            machine = "x86"

# some more fixes based on examples in https://en.wikipedia.org/wiki/Uname
if not is_64bit and machine in ("x86_64", "amd64"):
    if any([x in system for x in ("cygwin", "mingw", "msys")]):
        machine = "i686"
    else:
        machine = "i386"

return f"{system}_{machine}"

I unfortunately don't have access to a M1 Mac to double check. If someone could confirm that this works, that would be great.

闻呓 2025-01-22 15:52:30

我可以通过运行以下命令来获取它:

python3 -c "import platform; print(platform.processor())"

输出如下:

在 M1/M2 Mac 上 --> arm

在较旧的 Mac 上 --> i386

I could get it by running this:

python3 -c "import platform; print(platform.processor())"

The output is as following:

On an M1/M2 Mac --> arm

On an older Mac --> i386

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