Last Updated: September 09, 2019
·
1.645K
· squioc

Get your linux distribution

Sometime your scripts require to know on which linux distribution they're running.

Thanks to the platform module of python, you can, easily, get this information:

python -c 'import platform; print platform.linux_distribution()[0]'

WARN: By default, the method is limited to a set of distributions. To support more distributions, you have to overwrite this list as follow:

python -c 'import platform; print platform.linux_distribution(supported_dists=(<my set of distribution>))[0]'

7 Responses
Add your response

does not work everywhere

over 1 year ago ·

Usually, I do
$cat /etc/*release

But it does not work for debian systems. It seems that you would need to do:
$ cat /etc/debian_version
6.0.7

over 1 year ago ·

@antigluk the method seems to read /etc/lsb-release according to this stackoverflow thread. which distribution do you use?

over 1 year ago ·

@squioc on arch-linux (manjaro distribution). /etc/lsb-release exists, it contains

DISTRIB_ID=ManjaroLinux
DISTRIB_RELEASE=0.8.5
DISTRIB_CODENAME=Ascella
DISTRIB_DESCRIPTION="Manjaro Linux"

but platform.linux_distribution() returns

('', '', '')
over 1 year ago ·

@antigluk Done. By default, the method is limited to a set of distributions. To support more distributions (or others distributions), you have to overwrite this list. I will edit the tip to add a note.

over 1 year ago ·

@squioc hmm. interesting. but why this limitation is exists at all?

over 1 year ago ·

@antigluk No ideas. This limitation exists since the initial commit of the module in 2003.

over 1 year ago ·