distro#

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 a renewed alternative implementation 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.7 is expected to remove it altogether. Its predecessor function platform.dist() was already deprecated since Python 2.6 and is also expected to be removed in Python 3.7. Still, there are many cases in which access to Linux distribution information is needed. See Python issue 1322 for more information.

Classes#

LinuxDistribution

Provides information about a Linux distribution.

Functions#

linux_distribution([full_distribution_name])

Return information about the current Linux distribution as a tuple

id()

Return the distro ID of the current Linux distribution, as a

name([pretty])

Return the name of the current Linux distribution, as a human-readable

version([pretty, best])

Return the version of the current Linux distribution, as a human-readable

version_parts([best])

Return the version of the current Linux distribution as a tuple

major_version([best])

Return the major version of the current Linux distribution, as a string,

minor_version([best])

Return the minor version of the current Linux distribution, as a string,

build_number([best])

Return the build number of the current Linux distribution, as a string,

like()

Return a space-separated list of distro IDs of distributions that are

codename()

Return the codename for the release of the current Linux distribution,

info([pretty, best])

Return certain machine-readable information items about the current Linux

os_release_info()

Return a dictionary containing key-value pairs for the information items

lsb_release_info()

Return a dictionary containing key-value pairs for the information items

distro_release_info()

Return a dictionary containing key-value pairs for the information items

os_release_attr(attribute)

Return a single named information item from the os-release file data source

lsb_release_attr(attribute)

Return a single named information item from the lsb_release command output

distro_release_attr(attribute)

Return a single named information item from the distro release file

main()

Attributes#

_UNIXCONFDIR

_OS_RELEASE_BASENAME

NORMALIZED_OS_ID

NORMALIZED_LSB_ID

NORMALIZED_DISTRO_ID

_DISTRO_RELEASE_CONTENT_REVERSED_PATTERN

_DISTRO_RELEASE_BASENAME_PATTERN

_DISTRO_RELEASE_IGNORE_BASENAMES

_distro

_UNIXCONFDIR#
_OS_RELEASE_BASENAME = 'os-release'#
NORMALIZED_OS_ID#
NORMALIZED_LSB_ID#
NORMALIZED_DISTRO_ID#
_DISTRO_RELEASE_CONTENT_REVERSED_PATTERN#
_DISTRO_RELEASE_BASENAME_PATTERN#
_DISTRO_RELEASE_IGNORE_BASENAMES = ('debian_version', 'lsb-release', 'oem-release')#
linux_distribution(full_distribution_name=True)#

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

  • id_name: If full_distribution_name is false, the result of distro.id(). Otherwise, the result of distro.name().

  • version: The result of distro.version().

  • codename: The result of distro.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 Linux 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 Linux distributions.

id()#

Return the distro ID of the current Linux distribution, as a machine-readable string.

For a number of Linux 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

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 Linux 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.

name(pretty=False)#

Return the name of the current Linux 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.

version(pretty=False, best=False)#

Return the version of the current Linux 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.

version_parts(best=False)#

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

  • major: The result of distro.major_version().

  • minor: The result of distro.minor_version().

  • build_number: The result of distro.build_number().

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

major_version(best=False)#

Return the major version of the current Linux 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.

minor_version(best=False)#

Return the minor version of the current Linux 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.

build_number(best=False)#

Return the build number of the current Linux 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.

like()#

Return a space-separated list of distro IDs of distributions that are closely related to the current Linux 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.

codename()#

Return the codename for the release of the current Linux 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.

info(pretty=False, best=False)#

Return certain machine-readable information items about the current Linux 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 of distro.id().

  • version: The result of distro.version().

  • version_parts -> major: The result of distro.major_version().

  • version_parts -> minor: The result of distro.minor_version().

  • version_parts -> build_number: The result of distro.build_number().

  • like: The result of distro.like().

  • codename: The result of distro.codename().

For a description of the pretty and best parameters, see the distro.version() method.

os_release_info()#

Return a dictionary containing key-value pairs for the information items from the os-release file data source of the current Linux distribution.

See `os-release file`_ for details about these information items.

lsb_release_info()#

Return a dictionary containing key-value pairs for the information items from the lsb_release command data source of the current Linux distribution.

See `lsb_release command output`_ for details about these information items.

distro_release_info()#

Return a dictionary containing key-value pairs for the information items from the distro release file data source of the current Linux distribution.

See `distro release file`_ for details about these information items.

os_release_attr(attribute)#

Return a single named information item from the os-release file data source of the current Linux 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.

lsb_release_attr(attribute)#

Return a single named information item from the lsb_release command output data source of the current Linux 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_release_attr(attribute)#

Return a single named information item from the distro release file data source of the current Linux 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.

class LinuxDistribution(include_lsb=True, os_release_file='', distro_release_file='')#

Bases: object

Provides information about a Linux 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 Linux 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.

__repr__()#

Return repr of all info

linux_distribution(full_distribution_name=True)#

Return information about the Linux distribution that is compatible with Python's platform.linux_distribution(), supporting a subset of its parameters.

For details, see distro.linux_distribution().

id()#

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

For details, see distro.id().

name(pretty=False)#

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

For details, see distro.name().

version(pretty=False, best=False)#

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

For details, see distro.version().

version_parts(best=False)#

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

For details, see distro.version_parts().

major_version(best=False)#

Return the major version number of the current distribution.

For details, see distro.major_version().

minor_version(best=False)#

Return the minor version number of the Linux distribution.

For details, see distro.minor_version().

build_number(best=False)#

Return the build number of the Linux distribution.

For details, see distro.build_number().

like()#

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

For details, see distro.like().

codename()#

Return the codename of the Linux distribution.

For details, see distro.codename().

info(pretty=False, best=False)#

Return certain machine-readable information about the Linux distribution.

For details, see distro.info().

os_release_info()#

Return a dictionary containing key-value pairs for the information items from the os-release file data source of the Linux distribution.

For details, see distro.os_release_info().

lsb_release_info()#

Return a dictionary containing key-value pairs for the information items from the lsb_release command data source of the Linux distribution.

For details, see distro.lsb_release_info().

distro_release_info()#

Return a dictionary containing key-value pairs for the information items from the distro release file data source of the Linux distribution.

For details, see distro.distro_release_info().

os_release_attr(attribute)#

Return a single named information item from the os-release file data source of the Linux distribution.

For details, see distro.os_release_attr().

lsb_release_attr(attribute)#

Return a single named information item from the lsb_release command output data source of the Linux distribution.

For details, see distro.lsb_release_attr().

distro_release_attr(attribute)#

Return a single named information item from the distro release file data source of the Linux distribution.

For details, see distro.distro_release_attr().

_get_os_release_info()#

Get the information items from the specified os-release file.

Returns:

A dictionary containing all information items.

static _parse_os_release_content(lines)#

Parse the lines of an os-release file.

Parameters:

  • lines: Iterable through the lines in the os-release file.

    Each line must be a unicode string or a UTF-8 encoded byte string.

Returns:

A dictionary containing all information items.

_get_lsb_release_info()#

Get the information items from the lsb_release command output.

Returns:

A dictionary containing all information items.

static _parse_lsb_release_content(lines)#

Parse the output of the lsb_release command.

Parameters:

  • lines: Iterable through the lines of the lsb_release output.

    Each line must be a unicode string or a UTF-8 encoded byte string.

Returns:

A dictionary containing all information items.

_get_distro_release_info()#

Get the information items from the specified distro release file.

Returns:

A dictionary containing all information items.

_parse_distro_release_file(filepath)#

Parse a distro release file.

Parameters:

  • filepath: Path name of the distro release file.

Returns:

A dictionary containing all information items.

static _parse_distro_release_content(line)#

Parse a line from a distro release file.

Parameters: * line: Line from the distro release file. Must be a unicode string

or a UTF-8 encoded byte string.

Returns:

A dictionary containing all information items.

_distro#
main()#