distro package (Linux Distribution) version 1.9.0¶
Official distro repository: distro official repo
Overview and motivation¶
If you want to jump into the API description right away, read about the consolidated accessor functions.
Compatibility¶
The distro
package is supported on Python 3.6+ and PyPy, and on any Linux
or BSD distribution that provides one or more of the data sources used by
this package.
This package is tested on Python 3.6+ and PyPy, with test data that mimics the exact behavior of the data sources of a number of Linux distributions.
If you want to add test data for more distributions, please create an issue in the distro issue tracker and provide the following information in the issue:
The os-release file /etc/os-release if present, with a fall-back on /usr/lib/os-release if needed.
The file names and content of the /etc/*release and /etc/*version files, if any.
The output of the command: lsb_release -a, if available.
The file names and content of any other files you are aware of that provide useful information about the distro.
There are already some open issues on missing test data.
Data sources¶
The distro
package implements a robust and inclusive way of retrieving the
information about a Linux distribution based on new standards and old methods,
namely from these data sources:
The os-release file, if present.
The lsb_release command output, if the lsb_release command is available.
The distro release file, if present.
The
uname
command output, if present.
Access to the information¶
This package provides three ways to access the information about a Linux distribution:
Consolidated accessor functions
These are module-global functions that take into account all data sources in a priority order, and that return information about the current Linux distribution.
These functions should be the normal way to access the information.
The precedence of data sources is applied for each information item separately. Therefore, it is possible that not all information items returned by these functions come from the same data source. For example, on a distribution that has an lsb_release command that returns the “Distributor ID” field but not the “Codename” field, and that has a distro release file that specifies a codename inside, the distro ID will come from the lsb_release command (because it has higher precedence), and the codename will come from the distro release file (because it is not provided by the lsb_release command).
Examples:
distro.id()
for retrieving the distro ID, ordistro.info()
to get the machine-readable part of the information in a more aggregated way, ordistro.linux_distribution()
with an interface that is compatible to the originalplatform.linux_distribution()
function, supporting a subset of its parameters.Single source accessor functions
These are module-global functions that take into account a single data source, and that return information about the current Linux distribution.
They are useful for distributions that provide multiple inconsistent data sources, or for retrieving information items that are not provided by the consolidated accessor functions.
Examples:
distro.os_release_attr()
for retrieving a single information item from the os-release data source, ordistro.lsb_release_info()
for retrieving all information items from the lsb_release command output data source.-
The
distro.LinuxDistribution
class provides the main code of this package.This package contains a private module-global
distro.LinuxDistribution
instance with default initialization arguments, that is used by the consolidated and single source accessor functions.A user-defined instance of the
distro.LinuxDistribution
class allows specifying the path names of the os-release file and distro release file and whether the lsb_release command should be used or not. That is useful for example when the distribution information from a chrooted environment is to be retrieved, or when a distro has multiple distro release files and the default algorithm uses the wrong one.
Consolidated accessor functions¶
This section describes the consolidated accessor functions. See access to the information for a discussion of the different kinds of accessor functions.
- distro.linux_distribution(full_distribution_name: bool = True) Tuple[str, str, str] [source]¶
Deprecated since version 1.6.0:
distro.linux_distribution()
is deprecated. It should only be used as a compatibility shim with Python’splatform.linux_distribution()
. Please usedistro.id()
,distro.version()
anddistro.name()
instead.Return information about the current OS distribution as a tuple
(id_name, version, codename)
with items as follows:id_name
: If full_distribution_name is false, the result ofdistro.id()
. Otherwise, the result ofdistro.name()
.version
: The result ofdistro.version()
.codename
: The extra item (usually in parentheses) after the os-release version number, or the result ofdistro.codename()
.
The interface of this function is compatible with the original
platform.linux_distribution()
function, supporting a subset of its parameters.The data it returns may not exactly be the same, because it uses more data sources than the original function, and that may lead to different data if the OS distribution is not consistent across multiple data sources it provides (there are indeed such distributions …).
Another reason for differences is the fact that the
distro.id()
method normalizes the distro ID string to a reliable machine-readable value for a number of popular OS distributions.
- distro.id() str [source]¶
Return the distro ID of the current distribution, as a machine-readable string.
For a number of OS distributions, the returned distro ID value is reliable, in the sense that it is documented and that it does not change across releases of the distribution.
This package maintains the following reliable distro ID values:
Distro ID
Distribution
“ubuntu”
Ubuntu
“debian”
Debian
“rhel”
RedHat Enterprise Linux
“centos”
CentOS
“fedora”
Fedora
“sles”
SUSE Linux Enterprise Server
“opensuse”
openSUSE
“amzn”
Amazon Linux
“arch”
Arch Linux
“buildroot”
Buildroot
“cloudlinux”
CloudLinux OS
“exherbo”
Exherbo Linux
“gentoo”
GenToo Linux
“ibm_powerkvm”
IBM PowerKVM
“kvmibm”
KVM for IBM z Systems
“linuxmint”
Linux Mint
“mageia”
Mageia
“mandriva”
Mandriva Linux
“parallels”
Parallels
“pidora”
Pidora
“raspbian”
Raspbian
“oracle”
Oracle Linux (and Oracle Enterprise Linux)
“scientific”
Scientific Linux
“slackware”
Slackware
“xenserver”
XenServer
“openbsd”
OpenBSD
“netbsd”
NetBSD
“freebsd”
FreeBSD
“midnightbsd”
MidnightBSD
“rocky”
Rocky Linux
“aix”
AIX
“guix”
Guix System
“altlinux”
ALT Linux
If you have a need to get distros for reliable IDs added into this set, or if you find that the
distro.id()
function returns a different distro ID for one of the listed distros, please create an issue in the distro issue tracker.Lookup hierarchy and transformations:
First, the ID is obtained from the following sources, in the specified order. The first available and non-empty value is used:
the value of the “ID” attribute of the os-release file,
the value of the “Distributor ID” attribute returned by the lsb_release command,
the first part of the file name of the distro release file,
The so determined ID value then passes the following transformations, before it is returned by this method:
it is translated to lower case,
blanks (which should not be there anyway) are translated to underscores,
a normalization of the ID is performed, based upon normalization tables. The purpose of this normalization is to ensure that the ID is as reliable as possible, even across incompatible changes in the OS distributions. A common reason for an incompatible change is the addition of an os-release file, or the addition of the lsb_release command, with ID values that differ from what was previously determined from the distro release file name.
- distro.name(pretty: bool = False) str [source]¶
Return the name of the current OS distribution, as a human-readable string.
If pretty is false, the name is returned without version or codename. (e.g. “CentOS Linux”)
If pretty is true, the version and codename are appended. (e.g. “CentOS Linux 7.1.1503 (Core)”)
Lookup hierarchy:
The name is obtained from the following sources, in the specified order. The first available and non-empty value is used:
If pretty is false:
the value of the “NAME” attribute of the os-release file,
the value of the “Distributor ID” attribute returned by the lsb_release command,
the value of the “<name>” field of the distro release file.
If pretty is true:
the value of the “PRETTY_NAME” attribute of the os-release file,
the value of the “Description” attribute returned by the lsb_release command,
the value of the “<name>” field of the distro release file, appended with the value of the pretty version (“<version_id>” and “<codename>” fields) of the distro release file, if available.
- distro.version(pretty: bool = False, best: bool = False) str [source]¶
Return the version of the current OS distribution, as a human-readable string.
If pretty is false, the version is returned without codename (e.g. “7.0”).
If pretty is true, the codename in parenthesis is appended, if the codename is non-empty (e.g. “7.0 (Maipo)”).
Some distributions provide version numbers with different precisions in the different sources of distribution information. Examining the different sources in a fixed priority order does not always yield the most precise version (e.g. for Debian 8.2, or CentOS 7.1).
Some other distributions may not provide this kind of information. In these cases, an empty string would be returned. This behavior can be observed with rolling releases distributions (e.g. Arch Linux).
The best parameter can be used to control the approach for the returned version:
If best is false, the first non-empty version number in priority order of the examined sources is returned.
If best is true, the most precise version number out of all examined sources is returned.
Lookup hierarchy:
In all cases, the version number is obtained from the following sources. If best is false, this order represents the priority order:
the value of the “VERSION_ID” attribute of the os-release file,
the value of the “Release” attribute returned by the lsb_release command,
the version number parsed from the “<version_id>” field of the first line of the distro release file,
the version number parsed from the “PRETTY_NAME” attribute of the os-release file, if it follows the format of the distro release files.
the version number parsed from the “Description” attribute returned by the lsb_release command, if it follows the format of the distro release files.
- distro.version_parts(best: bool = False) Tuple[str, str, str] [source]¶
Return the version of the current OS distribution as a tuple
(major, minor, build_number)
with items as follows:major
: The result ofdistro.major_version()
.minor
: The result ofdistro.minor_version()
.build_number
: The result ofdistro.build_number()
.
For a description of the best parameter, see the
distro.version()
method.
- distro.major_version(best: bool = False) str [source]¶
Return the major version of the current OS distribution, as a string, if provided. Otherwise, the empty string is returned. The major version is the first part of the dot-separated version string.
For a description of the best parameter, see the
distro.version()
method.
- distro.minor_version(best: bool = False) str [source]¶
Return the minor version of the current OS distribution, as a string, if provided. Otherwise, the empty string is returned. The minor version is the second part of the dot-separated version string.
For a description of the best parameter, see the
distro.version()
method.
- distro.build_number(best: bool = False) str [source]¶
Return the build number of the current OS distribution, as a string, if provided. Otherwise, the empty string is returned. The build number is the third part of the dot-separated version string.
For a description of the best parameter, see the
distro.version()
method.
- distro.like() str [source]¶
Return a space-separated list of distro IDs of distributions that are closely related to the current OS distribution in regards to packaging and programming interfaces, for example distributions the current distribution is a derivative from.
Lookup hierarchy:
This information item is only provided by the os-release file. For details, see the description of the “ID_LIKE” attribute in the os-release man page.
- distro.codename() str [source]¶
Return the codename for the release of the current OS distribution, as a string.
If the distribution does not have a codename, an empty string is returned.
Note that the returned codename is not always really a codename. For example, openSUSE returns “x86_64”. This function does not handle such cases in any special way and just returns the string it finds, if any.
Lookup hierarchy:
the codename within the “VERSION” attribute of the os-release file, if provided,
the value of the “Codename” attribute returned by the lsb_release command,
the value of the “<codename>” field of the distro release file.
- distro.info(pretty: bool = False, best: bool = False) InfoDict [source]¶
Return certain machine-readable information items about the current OS distribution in a dictionary, as shown in the following example:
{ 'id': 'rhel', 'version': '7.0', 'version_parts': { 'major': '7', 'minor': '0', 'build_number': '' }, 'like': 'fedora', 'codename': 'Maipo' }
The dictionary structure and keys are always the same, regardless of which information items are available in the underlying data sources. The values for the various keys are as follows:
id
: The result ofdistro.id()
.version
: The result ofdistro.version()
.version_parts -> major
: The result ofdistro.major_version()
.version_parts -> minor
: The result ofdistro.minor_version()
.version_parts -> build_number
: The result ofdistro.build_number()
.like
: The result ofdistro.like()
.codename
: The result ofdistro.codename()
.
For a description of the pretty and best parameters, see the
distro.version()
method.
Single source accessor functions¶
This section describes the single source accessor functions. See access to the information for a discussion of the different kinds of accessor functions.
- distro.os_release_info() Dict[str, str] [source]¶
Return a dictionary containing key-value pairs for the information items from the os-release file data source of the current OS distribution.
See os-release file for details about these information items.
- distro.lsb_release_info() Dict[str, str] [source]¶
Return a dictionary containing key-value pairs for the information items from the lsb_release command data source of the current OS distribution.
See lsb_release command output for details about these information items.
- distro.distro_release_info() Dict[str, str] [source]¶
Return a dictionary containing key-value pairs for the information items from the distro release file data source of the current OS distribution.
See distro release file for details about these information items.
- distro.uname_info() Dict[str, str] [source]¶
Return a dictionary containing key-value pairs for the information items from the distro release file data source of the current OS distribution.
- distro.os_release_attr(attribute: str) str [source]¶
Return a single named information item from the os-release file data source of the current OS distribution.
Parameters:
attribute
(string): Key of the information item.
Returns:
(string): Value of the information item, if the item exists. The empty string, if the item does not exist.
See os-release file for details about these information items.
- distro.lsb_release_attr(attribute: str) str [source]¶
Return a single named information item from the lsb_release command output data source of the current OS distribution.
Parameters:
attribute
(string): Key of the information item.
Returns:
(string): Value of the information item, if the item exists. The empty string, if the item does not exist.
See lsb_release command output for details about these information items.
- distro.distro_release_attr(attribute: str) str [source]¶
Return a single named information item from the distro release file data source of the current OS distribution.
Parameters:
attribute
(string): Key of the information item.
Returns:
(string): Value of the information item, if the item exists. The empty string, if the item does not exist.
See distro release file for details about these information items.
- distro.uname_attr(attribute: str) str [source]¶
Return a single named information item from the distro release file data source of the current OS distribution.
Parameters:
attribute
(string): Key of the information item.
Returns:
- (string): Value of the information item, if the item exists.
The empty string, if the item does not exist.
LinuxDistribution class¶
This section describes the access via the distro.LinuxDistribution
class.
See access to the information for a discussion of the different kinds of
accessor functions.
- class distro.LinuxDistribution(include_lsb: bool | None = None, os_release_file: str = '', distro_release_file: str = '', include_uname: bool | None = None, root_dir: str | None = None, include_oslevel: bool | None = None)[source]¶
Provides information about a OS distribution.
This package creates a private module-global instance of this class with default initialization arguments, that is used by the consolidated accessor functions and single source accessor functions. By using default initialization arguments, that module-global instance returns data about the current OS distribution (i.e. the distro this package runs on).
Normally, it is not necessary to create additional instances of this class. However, in situations where control is needed over the exact data sources that are used, instances of this class can be created with a specific distro release file, or a specific os-release file, or without invoking the lsb_release command.
- build_number(best: bool = False) str [source]¶
Return the build number of the current distribution.
For details, see
distro.build_number()
.
- codename() str [source]¶
Return the codename of the OS distribution.
For details, see
distro.codename()
.
- distro_release_attr(attribute: str) str [source]¶
Return a single named information item from the distro release file data source of the OS distribution.
For details, see
distro.distro_release_attr()
.
- distro_release_info() Dict[str, str] [source]¶
Return a dictionary containing key-value pairs for the information items from the distro release file data source of the OS distribution.
For details, see
distro.distro_release_info()
.
- id() str [source]¶
Return the distro ID of the OS distribution, as a string.
For details, see
distro.id()
.
- info(pretty: bool = False, best: bool = False) InfoDict [source]¶
Return certain machine-readable information about the OS distribution.
For details, see
distro.info()
.
- like() str [source]¶
Return the IDs of distributions that are like the OS distribution.
For details, see
distro.like()
.
- linux_distribution(full_distribution_name: bool = True) Tuple[str, str, str] [source]¶
Return information about the OS distribution that is compatible with Python’s
platform.linux_distribution()
, supporting a subset of its parameters.For details, see
distro.linux_distribution()
.
- lsb_release_attr(attribute: str) str [source]¶
Return a single named information item from the lsb_release command output data source of the OS distribution.
For details, see
distro.lsb_release_attr()
.
- lsb_release_info() Dict[str, str] [source]¶
Return a dictionary containing key-value pairs for the information items from the lsb_release command data source of the OS distribution.
For details, see
distro.lsb_release_info()
.
- major_version(best: bool = False) str [source]¶
Return the major version number of the current distribution.
For details, see
distro.major_version()
.
- minor_version(best: bool = False) str [source]¶
Return the minor version number of the current distribution.
For details, see
distro.minor_version()
.
- name(pretty: bool = False) str [source]¶
Return the name of the OS distribution, as a string.
For details, see
distro.name()
.
- os_release_attr(attribute: str) str [source]¶
Return a single named information item from the os-release file data source of the OS distribution.
For details, see
distro.os_release_attr()
.
- os_release_info() Dict[str, str] [source]¶
Return a dictionary containing key-value pairs for the information items from the os-release file data source of the OS distribution.
For details, see
distro.os_release_info()
.
- uname_attr(attribute: str) str [source]¶
Return a single named information item from the uname command output data source of the OS distribution.
For details, see
distro.uname_attr()
.
- uname_info() Dict[str, str] [source]¶
Return a dictionary containing key-value pairs for the information items from the uname command data source of the OS distribution.
For details, see
distro.uname_info()
.
Normalization tables¶
These translation tables are used to normalize the parsed distro ID values
into reliable IDs. See distro.id()
for details.
They are documented in order to show for which distros a normalization is currently defined.
As a quick fix, these tables can also be extended by the user by appending new entries, should the need arise. If you have a need to get these tables extended, please make an according request in the distro issue tracker.
- distro.NORMALIZED_OS_ID = {'ol': 'oracle', 'opensuse-leap': 'opensuse'}¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s
(key, value) pairs
- dict(iterable) -> new dictionary initialized as if via:
d = {} for k, v in iterable:
d[k] = v
- dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
- distro.NORMALIZED_LSB_ID = {'enterpriseenterpriseas': 'oracle', 'enterpriseenterpriseserver': 'oracle', 'redhatenterprisecomputenode': 'rhel', 'redhatenterpriseserver': 'rhel', 'redhatenterpriseworkstation': 'rhel'}¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s
(key, value) pairs
- dict(iterable) -> new dictionary initialized as if via:
d = {} for k, v in iterable:
d[k] = v
- dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
- distro.NORMALIZED_DISTRO_ID = {'redhat': 'rhel'}¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s
(key, value) pairs
- dict(iterable) -> new dictionary initialized as if via:
d = {} for k, v in iterable:
d[k] = v
- dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
Os-release file¶
The os-release file is looked up using the path name /etc/os-release
. Its
optional additional location /usr/lib/os-release
would be looked up for if
the former couldn’t be read.
The os-release file is expected to be encoded in UTF-8.
It is parsed using the standard Python shlex
package, which treats it
like a shell script.
The attribute names found in the file are translated to lower case and then
become the keys of the information items from the os-release file data source.
These keys can be used to retrieve single items with the
distro.os_release_attr()
function, and they are also used as keys in the
dictionary returned by distro.os_release_info()
.
The attribute values found in the file are processed using shell rules (e.g. for whitespace, escaping, and quoting) before they become the values of the information items from the os-release file data source.
If the attribute “VERSION” is found in the file, the distro codename is extracted from its value if it can be found there. If a codename is found, it becomes an additional information item with key “codename”.
See the os-release man page for a list of possible attributes in the file.
Examples:
The following os-release file content:
NAME='Ubuntu' VERSION="14.04.3 LTS, Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04.3 LTS" VERSION_ID="14.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
results in these information items:
Key
Value
name
“Ubuntu”
version
“14.04.3 LTS, Trusty Tahr”
id
“ubuntu”
id_like
“debian”
pretty_name
“Ubuntu 14.04.3 LTS”
version_id
“14.04”
home_url
support_url
bug_report_url
codename
“Trusty Tahr”
The following os-release file content:
NAME="Red Hat Enterprise Linux Server" VERSION="7.0 (Maipo)" ID="rhel" ID_LIKE="fedora" VERSION_ID="7.0" PRETTY_NAME="Red Hat Enterprise Linux Server 7.0 (Maipo)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:redhat:enterprise_linux:7.0:GA:server" HOME_URL="https://www.redhat.com/" BUG_REPORT_URL="https://bugzilla.redhat.com/" REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7" REDHAT_BUGZILLA_PRODUCT_VERSION=7.0 REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux" REDHAT_SUPPORT_PRODUCT_VERSION=7.0
results in these information items:
Key
Value
name
“Red Hat Enterprise Linux Server”
version
“7.0 (Maipo)”
id
“rhel”
id_like
“fedora”
version_id
“7.0”
pretty_name
“Red Hat Enterprise Linux Server 7.0 (Maipo)”
ansi_color
“0;31”
cpe_name
“cpe:/o:redhat:enterprise_linux:7.0:GA:server”
home_url
bug_report_url
redhat_bugzilla_product
“Red Hat Enterprise Linux 7”
redhat_bugzilla_product_version
“7.0”
redhat_support_product
“Red Hat Enterprise Linux”
redhat_support_product_version
“7.0”
codename
“Maipo”
Lsb_release command output¶
The lsb_release command is expected to be in the PATH, and is invoked as follows:
lsb_release -a
The command output is expected to be encoded in UTF-8.
Only lines in the command output with the following format will be used:
<attr-name>: <attr-value>
Where:
<attr-name>
is the name of the attribute, and<attr-value>
is the attribute value.
The attribute names are stripped from surrounding blanks, any remaining blanks are translated to underscores, they are translated to lower case, and then become the keys of the information items from the lsb_release command output data source.
The attribute values are stripped from surrounding blanks, and then become the values of the information items from the lsb_release command output data source.
See the lsb_release man page for a description of standard attributes returned by the lsb_release command.
Examples:
The following lsb_release command output:
No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 14.04.3 LTS Release: 14.04 Codename: trusty
results in these information items:
Key
Value
distributor_id
“Ubuntu”
description
“Ubuntu 14.04.3 LTS”
release
“14.04”
codename
“trusty”
The following lsb_release command output:
LSB Version: n/a Distributor ID: SUSE LINUX Description: SUSE Linux Enterprise Server 12 SP1 Release: 12.1 Codename: n/a
results in these information items:
Key
Value
lsb_version
“n/a”
distributor_id
“SUSE LINUX”
description
“SUSE Linux Enterprise Server 12 SP1”
release
“12.1”
codename
“n/a”
Distro release file¶
Unless specified with a particular path name when using the
distro.LinuxDistribution
class, the distro release file is found by using
the first match in the alphabetically sorted list of the files matching the
following path name patterns:
/etc/*-release
/etc/*_release
/etc/*-version
/etc/*_version
where the following special path names are excluded:
/etc/debian_version
/etc/system-release
/etc/os-release
and where the first line within the file has the expected format.
The algorithm to sort the files alphabetically is far from perfect, but the distro release file has the least priority as a data source, and it is expected that distributions provide one of the other data sources.
The distro release file is expected to be encoded in UTF-8.
Only its first line is used, and it is expected to have the following format:
<name> [[[release] <version_id>] (<codename>)]
Where:
square brackets indicate optionality,
<name>
is the distro name,<version_id>
is the distro version, and<codename>
is the distro codename.
The following information items can be found in a distro release file (shown with their keys and data types):
id
(string): Distro ID, taken from the first part of the file name before the hyphen (-
) or underscore (_
).Note that the distro ID is not normalized or translated to lower case at this point; this happens only for the result of the
distro.id()
function.name
(string): Distro name, as found in the first line of the file.version_id
(string): Distro version, as found in the first line of the file. If not found, this information item will not exist.codename
(string): Distro codename, as found in the first line of the file. If not found, this information item will not exist.Note that the string in the codename field is not always really a codename. For example, openSUSE returns “x86_64”.
Examples:
The following distro release file
/etc/centos-release
:CentOS Linux release 7.1.1503 (Core)
results in these information items:
Key
Value
id
“centos”
name
“CentOS Linux”
version_id
“7.1.1503”
codename
“Core”
The following distro release file
/etc/oracle-release
:Oracle Linux Server release 7.1
results in these information items:
Key
Value
id
“oracle”
name
“Oracle Linux Server”
version_id
“7.1”
The following distro release file
/etc/SuSE-release
:openSUSE 42.1 (x86_64)
results in these information items:
Key
Value
id
“SuSE”
name
“openSUSE”
version_id
“42.1”
codename
“x86_64”