Distro - an OS platform information API
—
Low-level access to the underlying data sources that distro uses for distribution detection. These functions provide direct access to raw data from os-release files, lsb_release command, distro release files, and uname command.
Access to /etc/os-release or /usr/lib/os-release file contents.
def os_release_info() -> Dict[str, str]:
"""
Return the os-release information as a dictionary.
Reads from /etc/os-release with fallback to /usr/lib/os-release.
This is the modern standard for Linux distribution identification.
Returns:
Dict[str, str]: Key-value pairs from os-release file
Common keys include:
- ID: distribution identifier
- NAME: human-readable name
- VERSION: version string
- VERSION_ID: machine-readable version
- VERSION_CODENAME: release codename
- ID_LIKE: similar distributions
- PRETTY_NAME: formatted name with version
- HOME_URL: distribution homepage
- BUG_REPORT_URL: bug reporting URL
"""Usage example:
import distro
os_info = distro.os_release_info()
print(os_info)
# {
# 'ID': 'ubuntu',
# 'NAME': 'Ubuntu',
# 'VERSION': '20.04.3 LTS (Focal Fossa)',
# 'VERSION_ID': '20.04',
# 'VERSION_CODENAME': 'focal',
# 'ID_LIKE': 'debian',
# 'PRETTY_NAME': 'Ubuntu 20.04.3 LTS',
# 'HOME_URL': 'https://www.ubuntu.com/',
# 'BUG_REPORT_URL': 'https://bugs.launchpad.net/ubuntu/'
# }
# Access specific values
if 'ID' in os_info:
print(f"Distribution ID: {os_info['ID']}")Access to lsb_release command output.
def lsb_release_info() -> Dict[str, str]:
"""
Return the lsb_release information as a dictionary.
Executes 'lsb_release -a' command and parses output.
LSB (Linux Standard Base) compliance information.
Returns:
Dict[str, str]: Key-value pairs from lsb_release command
Common keys include:
- Distributor ID: distribution identifier
- Description: full description with version
- Release: version number
- Codename: release codename
"""Usage example:
import distro
lsb_info = distro.lsb_release_info()
print(lsb_info)
# {
# 'Distributor ID': 'Ubuntu',
# 'Description': 'Ubuntu 20.04.3 LTS',
# 'Release': '20.04',
# 'Codename': 'focal'
# }
# Check if LSB is available
if lsb_info:
print(f"LSB Description: {lsb_info.get('Description', 'N/A')}")
else:
print("LSB release information not available")Access to distribution-specific release files.
def distro_release_info() -> Dict[str, str]:
"""
Return the distro release file information as a dictionary.
Reads from distribution-specific release files like:
- /etc/redhat-release (Red Hat, CentOS, Fedora)
- /etc/debian_version (Debian)
- /etc/SuSE-release (openSUSE)
- /etc/arch-release (Arch Linux)
Returns:
Dict[str, str]: Parsed information from release files
Keys vary by distribution but commonly include:
- name: distribution name
- version: version string
- codename: release codename (if available)
"""Usage example:
import distro
release_info = distro.distro_release_info()
print(release_info)
# Example for CentOS:
# {
# 'name': 'CentOS Linux',
# 'version': '8.4.2105',
# 'codename': 'Core'
# }
# Check what release files were found
if release_info:
print(f"Release file info: {release_info}")
else:
print("No distro release files found")Access to uname command output, primarily for BSD systems.
def uname_info() -> Dict[str, str]:
"""
Return the uname information as a dictionary.
Executes 'uname -rs' command to get system information.
Primarily used for BSD-based systems where other methods may not be available.
Returns:
Dict[str, str]: System information from uname
Keys include:
- name: operating system name
- release: release version
"""Usage example:
import distro
uname_info = distro.uname_info()
print(uname_info)
# Example for FreeBSD:
# {
# 'name': 'FreeBSD',
# 'release': '13.0-RELEASE'
# }
# Useful for BSD detection
if 'BSD' in uname_info.get('name', ''):
print("BSD-based system detected")Direct access to specific attributes from each data source.
def os_release_attr(attribute: str) -> str:
"""
Return a single named attribute from os-release.
Args:
attribute: Name of the attribute to retrieve
Returns:
str: Attribute value, empty string if not found
"""
def lsb_release_attr(attribute: str) -> str:
"""
Return a single named attribute from lsb_release.
Args:
attribute: Name of the attribute to retrieve
Returns:
str: Attribute value, empty string if not found
"""
def distro_release_attr(attribute: str) -> str:
"""
Return a single named attribute from distro release file.
Args:
attribute: Name of the attribute to retrieve
Returns:
str: Attribute value, empty string if not found
"""
def uname_attr(attribute: str) -> str:
"""
Return a single named attribute from uname.
Args:
attribute: Name of the attribute to retrieve ('name' or 'release')
Returns:
str: Attribute value, empty string if not found
"""Usage examples:
import distro
# Get specific attributes from each source
os_id = distro.os_release_attr('ID')
lsb_desc = distro.lsb_release_attr('Description')
release_name = distro.distro_release_attr('name')
uname_name = distro.uname_attr('name')
print(f"OS Release ID: {os_id}")
print(f"LSB Description: {lsb_desc}")
print(f"Release name: {release_name}")
print(f"Uname name: {uname_name}")
# Fallback pattern
version = (distro.os_release_attr('VERSION_ID') or
distro.lsb_release_attr('Release') or
distro.distro_release_attr('version'))
print(f"Version from any source: {version}")Not all data sources are available on every system:
import distro
# Check data source availability
sources = {
'os-release': bool(distro.os_release_info()),
'lsb_release': bool(distro.lsb_release_info()),
'distro_release': bool(distro.distro_release_info()),
'uname': bool(distro.uname_info())
}
print("Available data sources:")
for source, available in sources.items():
status = "✓" if available else "✗"
print(f" {status} {source}")Data source functions return empty dictionaries or strings when:
Always check return values before using them:
import distro
# Safe access pattern
os_info = distro.os_release_info()
if os_info and 'ID' in os_info:
dist_id = os_info['ID']
else:
dist_id = "unknown"
print(f"Distribution: {dist_id}")Install with Tessl CLI
npx tessl i tessl/pypi-distro