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?
The most important line here is “PRETTY_NAME=”, which contains the name of the distribution and version number that you're currently using. In the following example, you can see that Ubuntu 20.04 LTS is currently running: The command “lsb_release -d” shows the current distribution and its version number.
The best way to determine a Linux distribution name and release version information is using cat /etc/os-release command, which works on almost all Linux system.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With