4

I would like to know how to determine the precise Linux distribution I am on (excluding version numbers), from within a Python script and define a variable as equal to it. Now, I should clarify and say that I have seen these two questions:

and neither one was helpful to me as, the first of these questions had answers that were very generalized and merely returned posix for all Linux distributions. The second question's answers weren't helpful as I sometimes operate on some more obscure distributions like Manjaro Linux and Sabayon Linux. The most applicable answer to the second question, was platform.linux_distribution(), which on Manjaro, returns:

('', '', '') 

which as you can see is not helpful. Now I know a way I can get half-way to an acceptable answer, as:

from subprocess import call call(["lsb_release", "-si"]) 

returns the output (on Manjaro Linux, of course):

ManjaroLinux 0 

but defining a variable:

a=call(["lsb_release", "-si"]) 

gives an a with the value:

>>> a 0 

6 Answers 6

4

The 'a' is just the exit status, try:

from subprocess import Popen, PIPE, STDOUT cmd = "lsb_release -si" p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) output = p.stdout.read() 
Sign up to request clarification or add additional context in comments.

5 Comments

For me output is returning b'ManjaroLinux\n', any way to strip away the b and \n?
This returns just the OS and no version in my case.
@MikeVella Returns "b'ManjaroLinux'".
just output.strip() then - the b prefix indicates that this is a bytes literal and the b is ignored - it will behave as a normal string, there's more detail on this here but you can almost certainly not worry about it.
as @Rolbrok has pointed out, output.decode().rstrip('\n') will work
2

Replace call with check_output.

from subprocess import check_output a = check_output(["lsb_release", "-si"]) 

1 Comment

Same as with Mike's answer, this provides no version for the OS (at least mine).
1
In [24]: open('/etc/lsb-release').readline().strip().split('=')[-1] Out[24]: 'LinuxMint' 

4 Comments

This is the one I will be accepting as it is the first one to give me the exact output I was looking for.
@BrentonHorne does this answer also print out the version of the OS in your case?
Nope, this question is just about the distro's name, not so much the version. I usually use rolling-release model distros (which Manjaro and Sabayon are) for which version numbers are irrelevant.
This piece of information should be explicitly added to the question.
1

What about open("/etc/issue","r").read()?

2 Comments

Returns for me 'Manjaro Linux \\r (\\n) (\\l)\n\n\n', if you can strip away ` \\r (\\n) (\\l)\n\n\n` from that I will accept this answer.
In my case this provides more info (and it's simpler) than using Mike's code. Mike's simply returns elementary OS, while this one returns 'elementary OS Freya \\n \\l\n'. There are garbage chars but it gives the OS version.
0

You can also try subprocess.check_output. Based on docs: "Run command with arguments and return its output as a byte string." Docs: https://docs.python.org/2/library/subprocess.html

Code:

a = subprocess.check_output(["lsb_release", "-si"]) 

In my case, output was:

'Ubuntu\n' 

Comments

0

As you can see from the link I linked to in the 2nd answer : https://github.com/easybuilders/easybuild/wiki/OS_flavor_name_version

you notice that platform.dist is a way better method of determining the current linux platform, however this is deprecated in python 3.5 and is gone in 3.8. It seems like there hardly is a good answer for this.

However weird OS'es will sometimes still not define this, but there is also absolutely no guarentee that lsb_release will be available on a system...

dist: ('redhat', '5.8', 'Final') dist: ('debian', '4.0', '') dist: ('centos', '6.3', 'Final') dist: ('fedora', '16', 'Verne') dist: ('SuSE', '11.3', 'x86_64') dist: ('Ubuntu', '12.10', 'quantal') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.