distro package (Linux Distribution) version 1.3.0

Official distro repository: distro official repo

Overview and motivation

The distro package (distro stands for Linux Distribution) provides information about the Linux distribution it runs on, such as a reliable machine-readable distro ID, or version information.

It is the recommended replacement for Python’s original platform.linux_distribution() function, but it provides much more functionality. An alternative implementation became necessary because Python 3.5 deprecated this function, and Python 3.8 will remove it altogether. Its predecessor function platform.dist() was already deprecated since Python 2.6 and will also be removed in Python 3.8. Still, there are many cases in which access to OS distribution information is needed. See Python issue 1322 for more information.

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 2.7, 3.4+ 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 2.7, 3.4+ 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 content of the /etc/os-release file, if any.
  • 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:

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, or ld.info() to get the machine-readable part of the information in a more aggregated way, or distro.linux_distribution() with an interface that is compatible to the original platform.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, or distro.lsb_release_info() for retrieving all information items from the lsb_release command output data source.

  • LinuxDistribution class

    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=True)[source]

Return information about the current OS distribution as a tuple (id_name, version, codename) with items as follows:

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()[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
“amazon” Amazon Linux
“arch” Arch Linux
“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

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=False)[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=False, best=False)[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).

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=False)[source]

Return the version of the current OS distribution as a tuple (major, minor, build_number) with items as follows:

For a description of the best parameter, see the distro.version() method.

distro.major_version(best=False)[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=False)[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=False)[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()[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()[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=False, best=False)[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:

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()[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()[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()[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.os_release_attr(attribute)[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)[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)[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.

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=True, os_release_file='', distro_release_file='', include_uname=True)[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.

The initialization method of this class gathers information from the available data sources, and stores that in private instance attributes. Subsequent access to the information items uses these private instance attributes, so that the data sources are read only once.

Parameters:

  • include_lsb (bool): Controls whether the lsb_release command output is included as a data source.

    If the lsb_release command is not available in the program execution path, the data source for the lsb_release command will be empty.

  • os_release_file (string): The path name of the os-release file that is to be used as a data source.

    An empty string (the default) will cause the default path name to be used (see os-release file for details).

    If the specified or defaulted os-release file does not exist, the data source for the os-release file will be empty.

  • distro_release_file (string): The path name of the distro release file that is to be used as a data source.

    An empty string (the default) will cause a default search algorithm to be used (see distro release file for details).

    If the specified distro release file does not exist, or if no default distro release file can be found, the data source for the distro release file will be empty.

  • include_name (bool): Controls whether uname command output is included as a data source. If the uname command is not available in the program execution path the data source for the uname command will be empty.

Public instance attributes:

  • os_release_file (string): The path name of the os-release file that is actually used as a data source. The empty string if no distro release file is used as a data source.
  • distro_release_file (string): The path name of the distro release file that is actually used as a data source. The empty string if no distro release file is used as a data source.
  • include_lsb (bool): The result of the include_lsb parameter. This controls whether the lsb information will be loaded.
  • include_uname (bool): The result of the include_uname parameter. This controls whether the uname information will be loaded.

Raises:

  • IOError: Some I/O issue with an os-release file or distro release file.
  • subprocess.CalledProcessError: The lsb_release command had some issue (other than not being available in the program execution path).
  • UnicodeError: A data source has unexpected characters or uses an unexpected encoding.
linux_distribution(full_distribution_name=True)[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().

id()[source]

Return the distro ID of the OS distribution, as a string.

For details, see distro.id().

name(pretty=False)[source]

Return the name of the OS distribution, as a string.

For details, see distro.name().

version(pretty=False, best=False)[source]

Return the version of the OS distribution, as a string.

For details, see distro.version().

version_parts(best=False)[source]

Return the version of the OS distribution, as a tuple of version numbers.

For details, see distro.version_parts().

major_version(best=False)[source]

Return the major version number of the current distribution.

For details, see distro.major_version().

minor_version(best=False)[source]

Return the minor version number of the current distribution.

For details, see distro.minor_version().

build_number(best=False)[source]

Return the build number of the current distribution.

For details, see distro.build_number().

like()[source]

Return the IDs of distributions that are like the OS distribution.

For details, see distro.like().

codename()[source]

Return the codename of the OS distribution.

For details, see distro.codename().

info(pretty=False, best=False)[source]

Return certain machine-readable information about the OS distribution.

For details, see distro.info().

os_release_info()[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().

lsb_release_info()[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().

distro_release_info()[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().

uname_info()[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().

os_release_attr(attribute)[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().

lsb_release_attr(attribute)[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().

distro_release_attr(attribute)[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().

uname_attr(attribute)[source]

Return a single named information item from the uname command output data source of the OS distribution.

For details, see distro.uname_release_attr().

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 = {}

Translation table for normalizing the “ID” attribute defined in os-release files, for use by the distro.id() method.

  • Key: Value as defined in the os-release file, translated to lower case, with blanks translated to underscores.
  • Value: Normalized value.
distro.NORMALIZED_LSB_ID = {'enterpriseenterprise': 'oracle', 'redhatenterpriseserver': 'rhel', 'redhatenterpriseworkstation': 'rhel'}

Translation table for normalizing the “Distributor ID” attribute returned by the lsb_release command, for use by the distro.id() method.

  • Key: Value as returned by the lsb_release command, translated to lower case, with blanks translated to underscores.
  • Value: Normalized value.
distro.NORMALIZED_DISTRO_ID = {'redhat': 'rhel'}

Translation table for normalizing the distro ID derived from the file name of distro release files, for use by the distro.id() method.

  • Key: Value as derived from the file name of a distro release file, translated to lower case, with blanks translated to underscores.
  • Value: Normalized value.

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 is ignored.

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:

  1. 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 http://www.ubuntu.com/
    support_url http://help.ubuntu.com/
    bug_report_url http://bugs.launchpad.net/ubuntu/
    codename “Trusty Tahr”
  2. 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 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”
    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:

  1. 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”
  2. 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:

  1. 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”
  2. 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”
  3. 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”