检查Linux发行版名称

Max*_*rai 29 python linux

我必须从Python脚本中获取Linux发行版名称.dist平台模块中有一个方法:

import platform
platform.dist()
Run Code Online (Sandbox Code Playgroud)

但在我的Arch Linux下它返回:

>>> platform.dist()
('', '', '')
Run Code Online (Sandbox Code Playgroud)

为什么?我怎么能得到这个名字?

PS.我必须检查分发是否是基于Debian的.


更新:我在这里找到了Python站点,自2.6以来,dist()已被弃用.

>>> platform.linux_distribution()
('', '', '')
Run Code Online (Sandbox Code Playgroud)

nc3*_*c3b 16

是我发现的:

platform.linux_distribution
Run Code Online (Sandbox Code Playgroud)

尝试确定Linux OS分发名称的名称.

它说platform.dist从2.6开始就被弃用了,你必须platform.linux_distribution在Python 2中使用它(但在Python 3.5中它也被弃用).


Bjo*_*rnD 11

这对我在Ubuntu上有用:

('Ubuntu', '10.04', 'lucid')
Run Code Online (Sandbox Code Playgroud)

然后strace,我常常找出平台模块正在做什么来查找分布,这是这一部分:

open("/etc/lsb-release", O_RDONLY|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=102, ...}) = 0
fstat64(3, {st_mode=S_IFREG|0644, st_size=102, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb76b1000
read(3, "DISTRIB_ID=Ubuntu\nDISTRIB_RELEAS"..., 8192) = 102
read(3, "", 4096)                       = 0
read(3, "", 8192)                       = 0
close(3)                                = 0
Run Code Online (Sandbox Code Playgroud)

所以,/etc/lsb-release包含这些信息,来自Ubuntu的Debian基础文件包.


nir*_*r0s 7

之所以platform.linux_distribution没有识别某些分布,是因为非标准化的方式分布提供了与自身相关的版本信息.

我写了一个名为distro(现在用于pip)的包,旨在取代distro.linux_distribution.它适用于许多发行版,这些发行版在使用时可能会返回奇怪或空元组platform.

https://github.com/nir0s/distro ( distro,在pypi上)

它提供了更精细的API来检索与分发相关的信息.

$ python
Python 2.7.12 (default, Nov  7 2016, 11:55:55) 
[GCC 6.2.1 20160830] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import distro
>>> distro.linux_distribution()
(u'Antergos Linux', '', u'ARCHCODE')
Run Code Online (Sandbox Code Playgroud)

顺便说一下,platform.linux_distribution将在Python 3.7中删除.


dun*_*can 6

它在这里工作.不,Arch Linux不是基于Debian的.

>>> import platform
>>> platform.dist()
('SuSE', '11.2', 'x86_64')
Run Code Online (Sandbox Code Playgroud)

所以Python不知道如何获取Arch Linux发布信息,并且它已经硬编码寻找/ etc/redhat-release和/ etc/SuSE-release.

platform.dist()是一个过时的函数.你应该使用platform.linux_distribution()

实际上,在我的系统上会产生不同的结果:

>>> platform.linux_distribution()
('openSUSE ', '11.2', 'x86_64')
Run Code Online (Sandbox Code Playgroud)

platform.linux_distribution()在/ etc文件中查找包含"release"或"version"的字符串.它还查看标准LSB版本文件.如果在最后不起作用,它会转向_dist_try_harder函数,该函数试图从其他地方获取信息.

因此,Arch Linux可以提供标准的LSB发布信息,也可以修补Python以使用它们的"方式".


Ash*_*ark 6

要在 python 中获取 Linux 发行版名称:

import distro
print(distro.id())
Run Code Online (Sandbox Code Playgroud)

我正在运行 Arch Linux,因此将返回:

arch
Run Code Online (Sandbox Code Playgroud)

有关可用的发行版 id 值,请参阅文档:https://distro.readthedocs.io/en/latest/