Check Linux distribution name
Asked Answered
C

15

43

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()
('', '', '')
Caliph answered 3/5, 2010 at 7:41 Comment(6)
@Kimvais I mean about getting name exactly from python script without parsing any files, only standart methods.Caliph
what does uname -a return on Arch? platform.py is 1600 lines of trying everything they could think of to distinguish various systems; it is a huge pile of heuristics. Arch also appears to be based only on itself, no other distro: en.wikipedia.org/wiki/Arch_LinuxStunt
What does lsb_release -is return under Arch? If platform.dist() gives you no usable data maybe you can call subprocess.check_output(["lsb_release","-is"]) instead.Sloshy
For googlers, here's a related question: How do I detect the Ubuntu version?Broadsword
@Ockonal FYI linux_distribution is deprecated since 3.5Perri
Possible duplicate of Python: What OS am I running on?Rustic
N
12

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.

Nobel answered 3/5, 2010 at 7:49 Comment(1)
Note that sometimes the file is /etc/os-release.Sloshy
F
20

Here's what I found:

platform.linux_distribution

Tries to determine the name of the Linux OS distribution name.

It says platform.dist is deprecated since 2.6, you have to use platform.linux_distribution in Python 2 (but it is also deprecated in Python 3.5).

Fender answered 3/5, 2010 at 7:45 Comment(1)
Unfortunately, platform.linux_distribution has been removed in python 3.8, but there's the third party package distro.Harlotry
S
16

The reason because of which platform.linux_distribution does not identify some distributions is the non-standardized way distributions provide version-related information on themselves.

I've written a package called distro (now used by pip) which aims to replace distro.linux_distribution. It works on many distributions which might return weird or empty tuples when using platform.

https://github.com/nir0s/distro (distro, on pypi)

It provides a much more elaborate API to retrieve distribution related information.

$ python
Python 2.7.12 (default, Nov  7 2016, 11:55:55) 
[GCC 6.2.1 20160830] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import distro
>>> distro.linux_distribution()
(u'Antergos Linux', '', u'ARCHCODE')

By the way, platform.linux_distribution is to be removed in Python 3.7.

Skipton answered 1/4, 2017 at 13:44 Comment(0)
N
12

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.

Nobel answered 3/5, 2010 at 7:49 Comment(1)
Note that sometimes the file is /etc/os-release.Sloshy
T
6

To get a linux distribution name in python:

import distro
print(distro.id())

I am running Arch Linux, so this will return:

arch

For available distro id values see documenteation: https://distro.readthedocs.io/en/latest/

Tollefson answered 9/7, 2020 at 9:46 Comment(0)
B
5

It works here. And no, Arch Linux is not Debian-based.

>>> import platform
>>> platform.dist()
('SuSE', '11.2', 'x86_64')

So Python does not know how to get the Arch Linux release information, and it has hardcoded looking for /etc/redhat-release and /etc/SuSE-release.

platform.dist() is an obsolete function. You should use platform.linux_distribution()

Actually, on my system it yields a different result:

>>> platform.linux_distribution()
('openSUSE ', '11.2', 'x86_64')

platform.linux_distribution() looks in /etc files containing "release" or "version" as string. It also looks in the standard LSB release file. If at the end that did not work, it resorts to a _dist_try_harder function which tries to get the information from other places.

So it is up to Arch Linux to provide a standard LSB release information or to patch Python to use their "way".

Boozy answered 3/5, 2010 at 7:56 Comment(0)
B
4

Now there is the platform.freedesktop_os_release() method that you can use. To my knowledge, it provides support for every Linux distribution, including Arch.

>>> import platform
>>> platform.freedesktop_os_release()
{'NAME': 'Arch Linux', 'ID': 'arch', 'PRETTY_NAME': 'Arch Linux', 'BUILD_ID': 'rolling', 'ANSI_COLOR': '38;2;23;147;209', 'HOME_URL': 'https://archlinux.org/', 'DOCUMENTATION_URL': 'https://wiki.archlinux.org/', 'SUPPORT_URL': 'https://bbs.archlinux.org/', 'BUG_REPORT_URL': 'https://bugs.archlinux.org/', 'LOGO': 'archlinux-logo'}

You should be using the NAME or ID entry in order to check for the distribution. Values can be changed on any Linux system, but these values are available by default.

Bodgie answered 13/9, 2022 at 19:4 Comment(1)
Note this is only available on Python 3.10 and higher (docs.python.org/3/library/…)Splutter
T
3

Here:

import sys, platform
print(sys.platform, platform.platform())
OUT [1]: linux Linux-5.4.44-1-MANJARO-x86_64-with-glibc2.2.5
Transhumance answered 15/7, 2020 at 20:8 Comment(0)
A
2

This worked for me under Ubuntu and Manjaro:

def get_distro():
    """
    Name of your Linux distro (in lowercase).
    """
    with open("/etc/issue") as f:
        return f.read().lower().split()[0]
Adjoining answered 30/5, 2015 at 18:50 Comment(2)
not good idea , /etc/issue is for show welcome line on standard console and very often is modified by user/admin. Standard way to detect modern distro is parse /etc/lsb-release or better /etc/os-releaseStupidity
for CentOS lsb-release is not available per default, so the /etc/issue works for me on more distributions as lsb-release ...Unisexual
C
2

Python 2 does not properly detect Arch Linux. This has been fixed in Python 3.3+, but was never back-ported to Python 2. Here are a couple of official bug reports:

https://bugs.python.org/issue20454
https://bugs.python.org/issue11678

A workaround for this issue in Python 2 is fairly simple. You just need to tell the platform module that "arch" is a supported distribution:

>>> import platform
>>> platform.linux_distribution(supported_dists=platform._supported_dists + ('arch',))
('arch', '', '')

Note that Arch Linux is a rolling release, so it does not have a version or id.

The supported_dists argument is documented here, although I don't find the documentation very clear. You don't want to overwrite _supported_dists because then your code will only work on Arch Linux. You want to append to the tuple.

In fact, if you print out the value of platform._supported_dists, you'll see that the only difference between Python 2.7.12 and Python 3.5.1 is the addition of ('arch', 'mageia'). Fun fact: you can also append 'system' for platform detection on Amazon Linux.

Cypripedium answered 4/11, 2016 at 22:11 Comment(0)
L
1

None of the above platform answers work with latest Python versions. I've tested with Python 3.8.10. I found following methods instead.

Using platform:

>>> import platform
>>> platform.version()
'#29~20.04.1-Ubuntu SMP Wed Aug 11 15:58:17 UTC 2021'

Using distro:

>>> import distro
>>> distro.id()
'ubuntu'

Ref: https://docs.python.org/3/library/platform.html#platform.linux_distribution

Lowrey answered 21/8, 2021 at 8:5 Comment(0)
H
0

Two options for you:

  1. Use import platform platform.linux_distribution() # Something like ('Ubuntu', '9.10', 'karmic')

  2. Or you could just read the contents of /etc/debian_version ("squeeze/sid") or /etc/lsb-release which would give:

    DISTRIB_ID=Ubunt
    DISTRIB_RELEASE=9.10
    DISTRIB_CODENAME=karmic
    DISTRIB_DESCRIPTION="Ubuntu 9.10"
    
Hoyt answered 3/5, 2010 at 7:49 Comment(1)
Unfortunately, the first option does not work on Arch Linux. I don't have access to Arch at the moment, so I can't test the second option.Cypripedium
A
0

If you want user readable data but still detailed, you can use platform.platform()

>>> import platform
>>> platform.platform()
'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'

Here's a few different possible calls you can make to identify where you are

import platform
import sys

def linux_distribution():
  try:
    return platform.linux_distribution()
  except:
    return "N/A"

print("""Python version: %s
dist: %s
linux_distribution: %s
system: %s
machine: %s
platform: %s
uname: %s
version: %s
mac_ver: %s
""" % (
sys.version.split('\n'),
str(platform.dist()),
linux_distribution(),
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
platform.mac_ver(),
))

The outputs of this script ran on a few different systems (Linux, Windows, Solaris, MacOS) and architectures (x86, x64, Itanium, power pc, sparc) is available here: https://github.com/hpcugent/easybuild/wiki/OS_flavor_name_version

Aldos answered 9/1, 2019 at 11:37 Comment(2)
platform.linux_distribution() is deprecated since 3.5Marysa
@Marysa yes, you should probably not use it or rely on it being there, since it's already in a try/except block here this code will not break after it is removed ;)Aldos
H
0

Up to date answer:

>>> import platform
>>>
>>> platform.uname()
uname_result(system='Linux', node='debian', release='5.10.0-15-amd64', version='#1 SMP Debian 5.10.120-1 (2022-06-09)', machine='x86_64')
>>>
>>> # or seperately
>>> platform.system()
'Linux'
>>> platform.node()
'debian'
>>> platform.release()
'5.10.0-15-amd64'
>>> platform.version()
'#1 SMP Debian 5.10.120-1 (2022-06-09)'
>>> platform.machine()
'x86_64'
>>> 
>>> # and also
>>> platform.platform()
'Linux-5.10.0-15-amd64-x86_64-with-glibc2.31'
>>> platform.architecture()
('64bit', 'ELF')
Heywood answered 26/8, 2022 at 12:39 Comment(0)
I
0

Many of the solutions do not work when executing inside a Container (The result is the host distro instead.)

A less elegant but container friendly approach:

from typing import Dict

def parse_env_file(path: str) -> Dict[str, str]:
    with open(path, 'r') as f:                                               
        return dict(tuple(line.replace('\n', '').split('=')) for line in f.readlines() if not line.startswith('#'))

print(parse_env_file("/etc/os-release")["NAME"])
Ignoble answered 18/3, 2023 at 14:40 Comment(0)
S
-1

You'll probably have to resort to:

if platform.linux_distribution() == ('', '', ''):
    # do something specific to look for Arch

or you could always augment lib/python2.6/platform.py and send in your changes.

Stunt answered 3/5, 2010 at 8:5 Comment(1)
This type of comparison is dangerous because Arch Linux is not the only distribution that platform cannot detect. Amazon Linux also results in an empty tuple.Cypripedium

© 2022 - 2024 — McMap. All rights reserved.