CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-distro

Distro - an OS platform information API

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Distro

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

Package Information

  • Package Name: distro
  • Language: Python
  • Installation: pip install distro
  • Version: 1.9.0
  • License: Apache License 2.0

Core Imports

import distro

For accessing specific functions directly:

from distro import id, name, version, info

For using the main class:

from distro import LinuxDistribution

Basic Usage

import 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}")

Architecture

Distro uses a hierarchical data source approach with fallback mechanisms:

  • Primary: /etc/os-release file (modern Linux standard)
  • Secondary: lsb_release command output (LSB-compliant systems)
  • Tertiary: Distribution-specific release files (/etc/*-release)
  • Fallback: 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.

Capabilities

Core Distribution Information

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

Core Distribution Information

Version Components

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

Version Components

Data Source Access

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

Data Source Access

LinuxDistribution Class

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

LinuxDistribution Class

Types

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]

Normalization Constants

The distro package uses normalization tables to map various distribution identifiers to standardized names for reliable cross-distribution compatibility.

OS Release ID Normalization

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.

LSB Release ID Normalization

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.

Distro Release File ID Normalization

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"

CLI Interface

Distro provides a command-line interface for system administration and scripting:

# Basic information
distro

# JSON output
distro -j

# Help
distro --help
def 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'.
    """

Legacy Compatibility

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)
    """
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/distro@1.9.x
Publish Source
CLI
Badge
tessl/pypi-distro badge