2011-12-01 20 views
8

我有一個運行這樣一個Python 3.2程序:確定如果Python是在Ubuntu正在運行Linux

import platform 
sysname = platform.system() 
sysver = platform.release() 
print(sysname+" "+sysver) 

和Windows返回:

的Windows 7

但在Ubuntu和其他回報:
Linux 3.0.0-13-generic

我需要這樣的東西:

Ubuntu 11.10或Mint 12

+2

http://docs.python.org/library/platform.html#platform.linux_distribution – Gazler

+0

謝謝,我真的應該讀該文件更多,呃? – triunenature

回答

5

嘗試platform.dist

>>> platform.dist() 
('Ubuntu', '11.10', 'oneiric') 
+4

platform.dist()現在已過時,我們應該使用platform.linux_distribution()(只需閱讀文檔,感謝@Gazler) – triunenature

+0

@triunenature:Python 3中不存在此類棄用警告。http:// docs .python.org/py3k/library/platform.html#platform.dist –

+1

相關知識:DI真的需要更仔細地閱讀這些文檔。 – triunenature

0

或者,你可以這樣做:

import sys 
sys.platform 

這將返回: '將linux2',或者你可以try..finally執行代碼塊。

+0

是的,但問題是,它只返回linux2,它沒有給我任何具體細節......而且這裏的目標是非常具體。 Ubuntu 10.04,或者其他,但只是告訴我它的Linux,或者postox並沒有幫助 – triunenature

6

當前接受的答案使用棄用函數。要做到這一點像Python 2.6和更高版本的正確方法是:

import platform 
print(platform.linux_distribution()) 

文檔不說,如果這個功能可在非Linux平臺上,但我的本地Windows桌面上,我得到:

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

還有這個,做同樣的事情在Win32的機器:

>>> print(platform.win32_ver()) 
('post2008Server', '6.1.7601', 'SP1', 'Multiprocessor Free') 
-1
is_ubuntu = 'ubuntu' in os.getenv('DESKTOP_SESSION', 'unknown') 

拿起如果你以U捉迷藏如果您正在尋找的是nity或Unity-2D。

+1

如果你在沒有桌面的Ubuntu服務器上運行,該怎麼辦? –

+0

這個想法不是在尋找ubuntu,它的映射哪個操作系統正在運行。 – triunenature

相關問題