2009-12-29 95 views
3

是否有一个现有的python模块可用于检测Linux的哪个发行版以及当前安装了哪个版本的发行版。Python模块检测Linux Distro版本

例如:

  • 红帽企业5
  • 的Fedora 11
  • SUSE企业版11
  • 等....

我可以让我自己通过分析各种模块像/ etc/redhat-release这样的文件,但我想知道一个模块是否已经存在?

干杯, 伊万

+0

相关http://unix.stackexchange.com/questions/92199/how-can-i-reliably-get-the-operating-systems-name – 2014-07-24 15:54:57

回答

14

查找的平台模块文档:http://docs.python.org/library/platform.html

例子:

 
>>> platform.uname() 
('Linux', 'localhost', '2.6.31.5-desktop-1mnb', '#1 SMP Fri Oct 23 00:05:22 EDT 2009', 'x86_64', 'AMD Athlon(tm) 64 X2 Dual Core Processor 3600+') 
>>> platform.linux_distribution() 
('Mandriva Linux', '2010.0', 'Official') 
+2

这两个函数都被弃用 – 2016-04-05 06:48:58

+0

不确定uname是,但linux_distribution肯定是,详细信息 - > https://bugs.python.org/issue1322 – wim 2016-08-16 15:43:42

2

以上回答不上RHEL 5.x的工作最快的方法是在类似redhat的系统上读取并查看/ etc/redhat-release文件。每次运行更新时都会更新此文件,并且系统会通过次要版本号进行升级。

$ python 
>>> open('/etc/redhat-release','r').read().split(' ')[6].split('.') 
['5', '5'] 

如果你拿出分开的部分,它只会给你的字符串。没有像你这样的模块问,但我认为它足够简短,你可能会觉得它很有用。

1

我写了一个名为distro(现在用于pip)的包,其目的是取代distro.linux_distribution。它适用于许多分布,当使用platform时可能会返回奇怪或空的元组。

https://github.com/nir0s/distrodistro,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') 

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