Reputation: 65923
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?
Upvotes: 11
Views: 12455
Reputation: 456
None of the solutions above worked for me or were too hacky. Fortunately, the standard library's platform
module as a function named freedesktop_os_release()
was quite handy! You can read more about it in the official docs at: platform.freedesktop_os_release()
Here is a simple example I needed recently to fetch the version code of a Ubuntu/Debian system in a Python script:
>>> import platform
>>> platform.freedesktop_os_release().get("VERSION_CODENAME")
# returns -> 'focal' on my Ubuntu 20.04 system
The Bash equivalent code for it would be something like this:
$ source /etc/os-release
$ echo "$VERSION_CODENAME"
# returns -> focal on my Ubuntu 20.04 system
Upvotes: 0
Reputation: 81936
One thing that you can do to simplify your code is to actually know one thing about how lsb_release
is written. It's actually written in python.
So we could reduce most of your code to this:
>>> import lsb_release
>>> lsb_release.get_lsb_information()
{'RELEASE': '10.04', 'CODENAME': 'lucid', 'ID': 'Ubuntu', 'DESCRIPTION': 'Ubuntu 10.04.4 LTS'}
This won't necessarily help with all of the sub-ubuntu distributions, but I don't know of any builtin table to do that for you.
Upvotes: 13
Reputation: 4366
def getOsFullDesc():
name = ''
if isfile('/etc/lsb-release'):
lines = open('/etc/lsb-release').read().split('\n')
for line in lines:
if line.startswith('DISTRIB_DESCRIPTION='):
name = line.split('=')[1]
if name[0]=='"' and name[-1]=='"':
return name[1:-1]
if isfile('/suse/etc/SuSE-release'):
return open('/suse/etc/SuSE-release').read().split('\n')[0]
try:
import platform
return ' '.join(platform.dist()).strip().title()
#return platform.platform().replace('-', ' ')
except ImportError:
pass
if os.name=='posix':
osType = os.getenv('OSTYPE')
if osType!='':
return osType
## sys.platform == 'linux2'
return os.name
Upvotes: 1
Reputation: 34
Also you can read: /etc/lsb-release or /etc/debian_version as text file
I use gentoo system, and for me:
# cat /etc/lsb-release
DISTRIB_ID="Gentoo"
Upvotes: 1
Reputation: 1533
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.
Upvotes: 9