本文介绍了如何检测Ubuntu版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个更改一些网络配置文件的 Python 应用程序.该应用程序需要在Ubuntu 10.04到13.10上运行.问题是,NetworkManager在不同版本上的损坏方式不同(尽管它们似乎最终已在13.04+中修复),这导致与我的应用程序不兼容.

I'm currently writing a Python app that changes some network configuration files. The app needs to run on Ubuntu 10.04 to 13.10. The problem is, that NetworkManager is broken in different ways on different versions (though they seem to have finally fixed it in 13.04+), and this causes incompatibilities with my app.

我已经找出了每个版本的问题,并为它们开发了变通办法,但我不确定最好的方法是检测用户正在运行的Ubuntu版本.

I've figured out the problems on each version and developed workarounds for them, I'm just not sure what the best way is to detect which version of Ubuntu the user is running.

到目前为止,我想出的最好的解决方案是解析 lsb_release -a 的输出,但这似乎是一个非常脆弱的解决方案,并且可能会因Ubuntu衍生的发行版而失败.薄荷,甚至可能带有某些官方"变体(Kubuntu,Xubuntu等).

The best solution I've come up with so far is to parse the output of lsb_release -a, but this seems to be a rather fragile solution and would probably fail with Ubuntu-derived distributions such as Mint and possibly even with some of the "official" variants (Kubuntu, Xubuntu, etc.).

是否有一种很好的方法来检测给定Linux发行版的 base 发行版和版本,以便我可以基于该版本对我的应用做出选择?

Is there a good way to detect the base distribution and version for a given Linux distribution so I can base the choices my app makes on that version?

推荐答案

最好的选择是使用os和平台库.

The best options are to use the os and platform libraries.

import os
import platform

print os.name #returns os name in simple form

platform.system() #returns the base system, in your case Linux
platform.release() #returns release version

平台库应该是更有用的.

The platform library should be the more useful one.

Rob在这篇文章中的评论也突出了更具体的平台.linux_distribution()认为我会在这里指出.

Rob's comment on this post also highlights the more specific platform.linux_distribution() thought I would point that out here.

这篇关于如何检测Ubuntu版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:55