Skip to content
Advertisement

How do I detect the Ubuntu version?

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.

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.

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.).

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?

Advertisement

Answer

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.

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement