检查Linux发行版名称

42

我需要从一个Python脚本中获取Linux发行版的名称。在platform模块中有一个dist方法:

import platform
platform.dist()

但是在我的 Arch Linux 上,它返回:

>>> platform.dist()
('', '', '')

为什么?我该如何获取名称?

附注:我必须检查发行版是否基于Debian。


更新:我在这里发现Python网站,dist()自2.6版起已弃用。

>>> platform.linux_distribution()
('', '', '')

@Kimvais 我的意思是在不解析任何文件的情况下,仅使用标准方法从Python脚本中精确获取名称。 - Max Frai
uname -a 在 Arch 上返回什么?platform.py 是 1600 行代码,尝试了所有可能的方法来区分各种系统;它是一个庞大的启发式堆积。Arch 似乎也只基于自身,没有其他发行版:http://en.wikipedia.org/wiki/Arch_Linux - msw
lsb_release -is 在 Arch 下返回什么?如果 platform.dist() 没有给你可用的数据,也许你可以调用 subprocess.check_output(["lsb_release","-is"]) - panzi
对于谷歌员工,这里有一个相关的问题:如何检测Ubuntu版本? - blong
4
FYI linux_distribution自3.5版本开始已被弃用。 - skyking
可能是Python:我正在运行什么操作系统?的重复问题。 - user7610
15个回答

0

许多解决方案在容器内执行时无法正常工作(结果是主机发行版)。

一种不太优雅但适用于容器的方法:

from typing import Dict

def parse_env_file(path: str) -> Dict[str, str]:
    with open(path, 'r') as f:                                               
        return dict(tuple(line.replace('\n', '').split('=')) for line in f.readlines() if not line.startswith('#'))

print(parse_env_file("/etc/os-release")["NAME"])

0

有两个选项供您选择:

  1. 使用 import platform platform.linux_distribution() # 类似于 ('Ubuntu', '9.10', 'karmic')

  2. 或者你可以直接读取 /etc/debian_version ("squeeze/sid") 或 /etc/lsb-release 的内容,会得到以下结果:

    DISTRIB_ID=Ubunt
    DISTRIB_RELEASE=9.10
    DISTRIB_CODENAME=karmic
    DISTRIB_DESCRIPTION="Ubuntu 9.10"
    

不幸的是,第一个选项在 Arch Linux 上无法工作。我目前没有访问 Arch 的权限,所以无法测试第二个选项。 - Adam Stewart

0

最新答案:

>>> import platform
>>>
>>> platform.uname()
uname_result(system='Linux', node='debian', release='5.10.0-15-amd64', version='#1 SMP Debian 5.10.120-1 (2022-06-09)', machine='x86_64')
>>>
>>> # or seperately
>>> platform.system()
'Linux'
>>> platform.node()
'debian'
>>> platform.release()
'5.10.0-15-amd64'
>>> platform.version()
'#1 SMP Debian 5.10.120-1 (2022-06-09)'
>>> platform.machine()
'x86_64'
>>> 
>>> # and also
>>> platform.platform()
'Linux-5.10.0-15-amd64-x86_64-with-glibc2.31'
>>> platform.architecture()
('64bit', 'ELF')

0
如果您想要用户可读的数据但仍然详细,您可以使用platform.platform()
>>> import platform
>>> platform.platform()
'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'

以下是几个可能的调用,可以用来确定您所在的位置

import platform
import sys

def linux_distribution():
  try:
    return platform.linux_distribution()
  except:
    return "N/A"

print("""Python version: %s
dist: %s
linux_distribution: %s
system: %s
machine: %s
platform: %s
uname: %s
version: %s
mac_ver: %s
""" % (
sys.version.split('\n'),
str(platform.dist()),
linux_distribution(),
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
platform.mac_ver(),
))

该脚本的输出在几个不同的系统(Linux、Windows、Solaris、MacOS)和架构(x86、x64、Itanium、Power PC、SPARC)上运行,可以在此处找到:https://github.com/hpcugent/easybuild/wiki/OS_flavor_name_version

1
platform.linux_distribution()自3.5版本起已被弃用。 - code_dredd
@code_dredd 是的,你可能不应该使用它或依赖它的存在,因为它已经在 try/except 块中了。在这里,即使它被移除,代码也不会崩溃 ;) - Jens Timmerman

-1

你可能需要采取以下措施:

if platform.linux_distribution() == ('', '', ''):
    # do something specific to look for Arch

或者你可以对 lib/python2.6/platform.py 进行修改并提交你的变更。


1
这种比较方式很危险,因为Arch Linux不是唯一一个platform无法检测到的发行版。Amazon Linux也会导致返回一个空元组。 - Adam Stewart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接