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



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

那么如何区分Python是按python3还是arch -x86_64 python3运行呢?

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

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进行仔细检查。如果有人可以确认这有效,那就太好了。

最新更新