如何确定主机系统使用哪种 libc 实现

问题描述 投票:0回答:3

在使用预构建的二进制文件和绑定生成器的 Python 设置代码中,我们检查操作系统和 CPU 架构,并相应地下载二进制文件。

现在,manylinux (glibc) 和 musllinux (musl) 都有二进制文件。我们如何找出主机系统使用哪个 libc 实现? 我知道

platform.libc_ver()
,但对于 musl 主机,它目前仅返回两个空字符串(请参阅 CPython 问题 87414)。

但是,必须有更精确的代码可用,因为

pip
需要方法来选择正确的轮子。

python linux glibc musl
3个回答
2
投票
from packaging.tags import sys_tags
tags = list(sys_tags())
print('musllinux' in tags[0].platform)

0
投票

这是我现在使用的解决方案。它受到 @AndrewNelson 的回答的启发,也使用了 platform 模块,但它被编写为

platform.libc_ver()
的覆盖层并直接调用
packaging._musllinux


self._libc_name, self._libc_ver = platform.libc_ver()

if self._libc_name.startswith("musl"):
    # try to be future proof in case libc_ver() gets musl support but uses "muslc" rather than just "musl"
    self._libc_name = "musl"

elif self._libc_name == "":
    try:
        import packaging._musllinux
        musl_ver = packaging._musllinux._get_musl_version(sys.executable)
        if musl_ver:
            self._libc_name, self._libc_ver = "musl", f"{musl_ver.major}.{musl_ver.minor}"
    except Exception as e:
        print(f"Musl detection failed: {e}", file=sys.stderr)

-1
投票

如果您只是检查 glibc 与 musl,为什么不直接:

import platform

def is_musl():
    libc, version = platform.libc_ver()
    return libc != "glibc"
© www.soinside.com 2019 - 2024. All rights reserved.