Friday, September 2, 2022

[SOLVED] Check Linux distribution name

Issue

I have to get the Linux distribution name from a Python script. There is a dist method in the platform module:

import platform
platform.dist()

But under my Arch Linux it returns:

>>> platform.dist()
('', '', '')

Why? How can I get the name?

PS. I have to check whether the distribution is Debian-based.


Update: I found here Python site, that dist() is deprecated since 2.6.

>>> platform.linux_distribution()
('', '', '')

Solution

This works for me on Ubuntu:

('Ubuntu', '10.04', 'lucid')

I then used strace to find out what exactly the platform module is doing to find the distribution, and it is this part:

open("/etc/lsb-release", O_RDONLY|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=102, ...}) = 0
fstat64(3, {st_mode=S_IFREG|0644, st_size=102, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb76b1000
read(3, "DISTRIB_ID=Ubuntu\nDISTRIB_RELEAS"..., 8192) = 102
read(3, "", 4096)                       = 0
read(3, "", 8192)                       = 0
close(3)                                = 0

So, there is /etc/lsb-release containing this information, which comes from Ubuntu's Debian base-files package.



Answered By - BjoernD
Answer Checked By - Katrina (WPSolving Volunteer)