Distro - an OS platform information API
npx @tessl/cli install tessl/pypi-distro@1.9.0A reliable and comprehensive API for retrieving information about the operating system distribution on Linux and BSD-based systems. It serves as the recommended replacement for Python's deprecated platform.linux_distribution function, offering machine-readable distribution IDs, version information, and codenames through multiple detection methods.
pip install distroimport distroFor accessing specific functions directly:
from distro import id, name, version, infoFor using the main class:
from distro import LinuxDistributionimport distro
# Get basic distribution information
print(f"Distribution ID: {distro.id()}")
print(f"Distribution Name: {distro.name()}")
print(f"Version: {distro.version()}")
print(f"Codename: {distro.codename()}")
# Get comprehensive information
info = distro.info()
print(f"Complete info: {info}")
# Pretty formatted output
print(f"Pretty name: {distro.name(pretty=True)}")
print(f"Pretty version: {distro.version(pretty=True)}")
# Version components
major, minor, build = distro.version_parts()
print(f"Version parts: {major}.{minor}.{build}")Distro uses a hierarchical data source approach with fallback mechanisms:
/etc/os-release file (modern Linux standard)lsb_release command output (LSB-compliant systems)/etc/*-release)uname command (BSD systems)The LinuxDistribution class encapsulates all detection logic with caching for performance. Module-level convenience functions use a global instance for simple access patterns.
Essential functions for identifying and describing the operating system distribution, including machine-readable IDs, human-readable names, version strings, and codenames.
def id() -> str: ...
def name(pretty: bool = False) -> str: ...
def version(pretty: bool = False, best: bool = False) -> str: ...
def codename() -> str: ...
def like() -> str: ...
def info(pretty: bool = False, best: bool = False) -> InfoDict: ...Functions for parsing and accessing individual components of version numbers, including major version, minor version, and build numbers.
def version_parts(best: bool = False) -> Tuple[str, str, str]: ...
def major_version(best: bool = False) -> str: ...
def minor_version(best: bool = False) -> str: ...
def build_number(best: bool = False) -> str: ...Low-level access to the underlying data sources that distro uses for distribution detection, including os-release files, lsb_release command, distro release files, and uname command.
def os_release_info() -> Dict[str, str]: ...
def lsb_release_info() -> Dict[str, str]: ...
def distro_release_info() -> Dict[str, str]: ...
def uname_info() -> Dict[str, str]: ...
def os_release_attr(attribute: str) -> str: ...
def lsb_release_attr(attribute: str) -> str: ...
def distro_release_attr(attribute: str) -> str: ...
def uname_attr(attribute: str) -> str: ...The main class that encapsulates all distribution detection logic with configurable data sources and custom file paths for testing or alternative environments.
class LinuxDistribution:
def __init__(
self,
include_lsb: Optional[bool] = None,
os_release_file: str = "",
distro_release_file: str = "",
include_uname: Optional[bool] = None,
root_dir: Optional[str] = None,
include_oslevel: Optional[bool] = None,
) -> None: ...from typing import TypedDict, Tuple, Dict, Optional
class VersionDict(TypedDict):
major: str
minor: str
build_number: str
class InfoDict(TypedDict):
id: str
version: str
version_parts: VersionDict
like: str
codename: str
# Package version
__version__: str
# Normalization constants
NORMALIZED_OS_ID: Dict[str, str]
NORMALIZED_LSB_ID: Dict[str, str]
NORMALIZED_DISTRO_ID: Dict[str, str]The distro package uses normalization tables to map various distribution identifiers to standardized names for reliable cross-distribution compatibility.
NORMALIZED_OS_ID = {
"ol": "oracle", # Oracle Linux
"opensuse-leap": "opensuse", # Newer versions of OpenSUSE report as opensuse-leap
}Maps ID values from /etc/os-release files to normalized distribution identifiers.
NORMALIZED_LSB_ID = {
"enterpriseenterpriseas": "oracle", # Oracle Enterprise Linux 4
"enterpriseenterpriseserver": "oracle", # Oracle Linux 5
"redhatenterpriseworkstation": "rhel", # RHEL 6, 7 Workstation
"redhatenterpriseserver": "rhel", # RHEL 6, 7 Server
"redhatenterprisecomputenode": "rhel", # RHEL 6 ComputeNode
}Maps Distributor ID values from lsb_release command output to normalized identifiers.
NORMALIZED_DISTRO_ID = {
"redhat": "rhel", # RHEL 6.x, 7.x
}Maps distribution identifiers derived from release file names (e.g., /etc/redhat-release) to normalized values.
Usage example:
import distro
# These normalization tables are used internally by distro.id()
# to ensure consistent identifiers across different data sources
dist_id = distro.id() # Returns normalized ID like "rhel" instead of "redhat"Distro provides a command-line interface for system administration and scripting:
# Basic information
distro
# JSON output
distro -j
# Help
distro --helpdef main() -> None:
"""
Command-line interface entry point for the distro tool.
Provides command-line access to distribution information with options for:
- Basic text output (default)
- JSON formatted output (-j/--json)
- Help information (--help)
This function is called when running 'distro' command or 'python -m distro'.
"""def linux_distribution(full_distribution_name: bool = True) -> Tuple[str, str, str]:
"""
DEPRECATED: Compatibility function for platform.linux_distribution()
Returns:
Tuple of (id_name, version, codename)
"""