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
Community
  • 1
  • 1
Josh Pinto
  • 1,453
  • 4
  • 20
  • 37

6 Answers6

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()
Mike Vella
  • 10,187
  • 14
  • 59
  • 86
  • For me `output` is returning `b'ManjaroLinux\n'`, any way to strip away the `b` and `\n`? – Josh Pinto Jan 14 '16 at 14:22
  • This returns just the OS and no version in my case. – Gabriel Jan 14 '16 at 14:25
  • @MikeVella Returns `"b'ManjaroLinux'"`. – Josh Pinto Jan 14 '16 at 14:26
  • 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](http://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal) but you can almost certainly not worry about it. – Mike Vella Jan 14 '16 at 14:29
  • as @Rolbrok has pointed out, output.decode().rstrip('\n') will work – Mike Vella Jan 14 '16 at 14:32
2

Replace call with check_output.

from subprocess import check_output
a = check_output(["lsb_release", "-si"])
dyeray
  • 1,056
  • 6
  • 17
1

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

J. Birkner
  • 56
  • 5
  • 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. – Josh Pinto Jan 14 '16 at 14:24
  • 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. – Gabriel Jan 14 '16 at 14:25
1
In [24]: open('/etc/lsb-release').readline().strip().split('=')[-1]
Out[24]: 'LinuxMint'
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93
  • This is the one I will be accepting as it is the first one to give me the exact output I was looking for. – Josh Pinto Jan 14 '16 at 14:27
  • @BrentonHorne does this answer also print out the version of the OS in your case? – Gabriel Jan 14 '16 at 14:28
  • 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. – Josh Pinto Jan 14 '16 at 14:28
  • This piece of information should be explicitly added to the question. – Gabriel Jan 14 '16 at 14:30
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'
Bish
  • 46
  • 6
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')
Jens Timmerman
  • 9,316
  • 1
  • 42
  • 48