Distro - an OS platform information API
—
Functions for parsing and accessing individual components of version numbers. These functions break down version strings into major, minor, and build number components for programmatic access.
Returns version as a tuple of individual components.
def version_parts(best: bool = False) -> Tuple[str, str, str]:
"""
Return version as tuple of (major, minor, build_number).
Args:
best: If True, use the most precise version from all data sources
Returns:
Tuple[str, str, str]: (major, minor, build_number)
Examples:
("20", "04", "") for "20.04"
("8", "4", "2105") for "8.4.2105"
("", "", "") if version cannot be parsed
"""Usage examples:
import distro
# Basic version parts
major, minor, build = distro.version_parts()
print(f"Version: {major}.{minor}.{build}") # "20.04."
# Using best available precision
major, minor, build = distro.version_parts(best=True)
if build:
print(f"Full version: {major}.{minor}.{build}") # "8.4.2105"
else:
print(f"Version: {major}.{minor}") # "20.04"Returns the major version number component.
def major_version(best: bool = False) -> str:
"""
Return the major version number.
Args:
best: If True, use the most precise version from all data sources
Returns:
str: Major version number as string, empty if not available
Examples:
"20" for Ubuntu 20.04
"8" for CentOS 8.4
"11" for Debian 11
"""Usage example:
import distro
major = distro.major_version()
print(f"Major version: {major}") # e.g., "20"
# Using best precision
major_best = distro.major_version(best=True)
print(f"Major (best): {major_best}")Returns the minor version number component.
def minor_version(best: bool = False) -> str:
"""
Return the minor version number.
Args:
best: If True, use the most precise version from all data sources
Returns:
str: Minor version number as string, empty if not available
Examples:
"04" for Ubuntu 20.04
"4" for CentOS 8.4
"0" for Debian 11.0
"""Usage example:
import distro
minor = distro.minor_version()
print(f"Minor version: {minor}") # e.g., "04"
# Check if minor version exists
if minor:
print(f"Full version: {distro.major_version()}.{minor}")
else:
print(f"Version: {distro.major_version()}")Returns the build number or patch level component.
def build_number(best: bool = False) -> str:
"""
Return the build number or patch level.
Args:
best: If True, use the most precise version from all data sources
Returns:
str: Build number as string, empty if not available
Examples:
"2105" for CentOS 8.4.2105
"3" for Ubuntu 20.04.3
"" for versions without build numbers
"""Usage examples:
import distro
build = distro.build_number()
if build:
print(f"Build number: {build}") # e.g., "2105"
else:
print("No build number available")
# Construct full version string
major = distro.major_version()
minor = distro.minor_version()
build = distro.build_number()
version_str = major
if minor:
version_str += f".{minor}"
if build:
version_str += f".{build}"
print(f"Constructed version: {version_str}") # e.g., "8.4.2105"The best parameter controls which data source is used for version information:
best=False): Uses the first available version from the primary data source (usually os-release)best=True): Searches all data sources and returns the most detailed/precise version availableimport distro
# Compare default vs best precision
default_version = distro.version()
best_version = distro.version(best=True)
print(f"Default: {default_version}") # "8.4"
print(f"Best: {best_version}") # "8.4.2105"
# Version parts comparison
default_parts = distro.version_parts()
best_parts = distro.version_parts(best=True)
print(f"Default parts: {default_parts}") # ("8", "4", "")
print(f"Best parts: {best_parts}") # ("8", "4", "2105")Version components are extracted from data sources in this priority order:
/etc/os-release or /usr/lib/os-release)/etc/redhat-release, /etc/debian_version)When best=True, all sources are checked and the most complete version information is returned.
Install with Tessl CLI
npx tessl i tessl/pypi-distro