python - 为什么 platform.linux_distribution() 在一个操作系统上返回不同的结果?

标签 python linux lsb

我在一些虚拟机 2.7.92.7.6 上安装了 2 个版本的 python。 2.7.6 是从系统包安装的,而 2.7.9 是从源安装的。这台机器在 Ubuntu 14.04 上运行。

我想使用 platform 模块来获取有关 linux 发行版的信息。然而事实证明,在这两个版本中,我得到了不同的 platform.linux_distribution() 结果。

Python 2.7.9 (...) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.linux_distribution()
('debian', 'jessie/sid', '')


Python 2.7.6 (...) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.linux_distribution()
('Ubuntu', '14.04', 'trusty')

知道为什么会这样吗? 或者更一般地说,平台模块如何获取有关 linux 分发的信息。它是基于 lsb_relase 或其他一些系统命令还是在某处进行了硬编码?

最佳答案

Ubuntu 14.04 包含两个发布文件:

# cat /etc/os-release 
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

root@yeni2:/# cat /etc/debian_version 
jessie/sid

两者都被函数 platform.linux_distribution() 使用,然而,这个函数被 Ubuntu 修补以返回 Ubuntu OS 名称,另见代码中的注释(右边是安装的文件Ubuntu,左边是来自 Python 2.7.10 中的源文件):

Python-2.7.10 # diff Lib/platform.py /mnt/ubu/\@/usr/lib/python2.7/platform.py
1c1
< #!/usr/bin/env python
---
> #! /usr/bin/python2.7
262c262
<     'UnitedLinux', 'turbolinux')
---
>     'UnitedLinux', 'turbolinux', 'Ubuntu')
290a291,294
> _distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I)
> _release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I)
> _codename_file_re = re.compile("(?:DISTRIB_CODENAME\s*=)\s*(.*)", re.I)
> 
314a319,337
>     # check for the LSB /etc/lsb-release file first, needed so
>     # that the distribution doesn't get identified as Debian.
>     try:
>         with open("/etc/lsb-release", "rU") as etclsbrel:
>             for line in etclsbrel:
>                 m = _distributor_id_file_re.search(line)
>                 if m:
>                     _u_distname = m.group(1).strip()
>                 m = _release_file_re.search(line)
>                 if m:
>                     _u_version = m.group(1).strip()
>                 m = _codename_file_re.search(line)
>                 if m:
>                     _u_id = m.group(1).strip()
>             if _u_distname and _u_version:
>                 return (_u_distname, _u_version, _u_id)
>     except (EnvironmentError, UnboundLocalError):
>         pass
> 

你的python 2.7.9编译源码,不包含来自Ubuntu的补丁,所以返回/etc/debian_version的内容

关于python - 为什么 platform.linux_distribution() 在一个操作系统上返回不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33996196/

相关文章:

python - 循环列表时如何附加连续元素?

python - 如何使此任务提高 cpu 使用率?

python - 从列列表中的括号内提取数字

linux - ansible - 通过 ansible-playbook 执行 InstallHalyard.sh 脚本时没有任何反应

linux - 在 shell unix 中查看操作系统版本

Systemd 单元,使用外部脚本检查状态

python 记录.Logger : overriding makeRecord

linux - 对 nvidia GPU 上的计算单元和预期核心的混淆

regex - Sed 仅删除第一次出现的字符串

python-3.x - 从python中的字节中提取LSB位