Montmons
Montmons

Reputation: 1426

Python 3.7 and above: how to determine Linux distribution?

Since the Python Docs clearly state that platform.linux_distribution() is:

Deprecated since version 3.5, will be removed in version 3.7.

What would be the correct and future-proof way to detect a Linux Distribution via Python?

Upvotes: 11

Views: 5019

Answers (3)

Mureinik
Mureinik

Reputation: 311273

This functionality will be removed from Python, as per Jim's answer. The distro package seems to be the recommended alternative:

$ pip3 install --user distro

$ python3
Python 3.6.3 (default, Oct  9 2017, 12:07:10) 
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import distro
>>> distro.linux_distribution()
('Fedora', '27', 'Twenty Seven')

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121864

You can use the distro project:

$ pip install distro
$ python
>>> import distro
>>> distro.linux_distribution(full_distribution_name=False)
('centos', '7.1.1503', 'Core')

This project came out of issue #1322, which led to the deprecation of the function. From the project README:

It is a renewed alternative implementation for Python's original platform.linux_distribution function, but it also provides much more functionality which isn't necessarily Python bound like a command-line interface

The method was removed from the platform library because the correct method to detect what distribution you were using changed faster than the Python release schedule could follow. From the above bug report:

The stdlib is not the right place for things that change this often. Just look at how many semi standards we've seen in the last few years. There's no point in trying to follow these in a slow moving code base as the Python stdlib. It's much better to put the functionality into a PyPI module which can be updated much more frequently.

Upvotes: 13

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160417

That will be left to a package. From the What's new entry for this change:

The platform.dist() and platform.linux_distribution() functions are now deprecated. Linux distributions use too many different ways of describing themselves, so the functionality is left to a package. (Contributed by Vajrasky Kok and Berker Peksag in bpo-1322.)

You can take a look at issue 1322 that removed it for a more detailed discussion, there's also a package there already.

The Python standard library won't be a place where you'll be able to do this due to the maintenance overhead it incurs.

Upvotes: 1

Related Questions