2009-12-29 93 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中被刪除。