ZetCode

Python os.uname Function

Last modified April 11, 2025

This comprehensive guide explores Python's os.uname function, which retrieves detailed system information. We'll cover its attributes, platform differences, and practical usage examples.

Basic Definitions

The os.uname function returns a named tuple containing system information. It's available on Unix-like systems but not on Windows.

The tuple contains: sysname (OS name), nodename (hostname), release (OS release), version (OS version), and machine (hardware identifier).

Basic System Information

The simplest use of os.uname retrieves all available system information. This example shows how to access each field individually.

basic_info.py
import os

# Get system information
system_info = os.uname()

print("Full system info:", system_info)
print("OS Name:", system_info.sysname)
print("Hostname:", system_info.nodename)
print("OS Release:", system_info.release)
print("OS Version:", system_info.version)
print("Hardware:", system_info.machine)

This code retrieves and displays all system information fields. The output varies depending on your operating system and hardware.

Note that os.uname() returns a named tuple, so fields can be accessed by either index or attribute name.

Checking Operating System

The sysname field identifies the operating system. This example demonstrates how to check the OS type and make decisions based on it.

check_os.py
import os

system_info = os.uname()
os_name = system_info.sysname.lower()

print(f"Running on: {os_name}")

if 'linux' in os_name:
    print("This is a Linux system")
elif 'darwin' in os_name:
    print("This is a macOS system")
elif 'freebsd' in os_name:
    print("This is a FreeBSD system")
else:
    print("Unknown or unsupported system")

This script identifies the operating system by checking the sysname field. The check is case-insensitive for robustness.

For cross-platform code, consider platform.system() which works on Windows too.

System Version Comparison

The release field contains the OS version number. This example shows how to parse and compare version numbers for compatibility checks.

version_check.py
import os
from distutils.version import StrictVersion

system_info = os.uname()
current_version = system_info.release

print(f"Current OS version: {current_version}")

# Compare with minimum required version
min_version = "5.4.0"

if StrictVersion(current_version) >= StrictVersion(min_version):
    print("System meets minimum version requirements")
else:
    print(f"System needs upgrade (minimum {min_version} required)")

This code uses StrictVersion to compare version numbers. It ensures the system meets minimum version requirements for compatibility.

For complex version strings, consider using packaging.version instead.

Hardware Architecture Detection

The machine field identifies the hardware architecture. This example shows how to detect the CPU architecture and handle different cases.

hardware_detect.py
import os

system_info = os.uname()
architecture = system_info.machine.lower()

print(f"Hardware architecture: {architecture}")

if 'x86_64' in architecture or 'amd64' in architecture:
    print("64-bit Intel/AMD processor")
elif 'arm' in architecture:
    print("ARM processor")
    if '64' in architecture:
        print("64-bit ARM")
    else:
        print("32-bit ARM")
elif 'aarch64' in architecture:
    print("64-bit ARM (AArch64)")
else:
    print("Unknown architecture")

This script identifies the processor architecture by checking the machine field. It handles common architectures and their variants.

For more detailed hardware info, consider platform.machine() as an alternative.

System Information Logging

This example demonstrates logging comprehensive system information for debugging or system inventory purposes.

system_logger.py
import os
import json
from datetime import datetime

def get_system_info():
    info = os.uname()
    return {
        "timestamp": datetime.now().isoformat(),
        "os_name": info.sysname,
        "hostname": info.nodename,
        "os_release": info.release,
        "os_version": info.version,
        "architecture": info.machine,
        "python_version": os.sys.version
    }

system_data = get_system_info()

print("System Information:")
print(json.dumps(system_data, indent=2))

# Save to file
with open("system_info.json", "w") as f:
    json.dump(system_data, f, indent=2)
    print("System info saved to system_info.json")

This code collects system information and stores it in a structured JSON format. The data includes both OS and Python version details.

The JSON format makes it easy to parse and analyze the data later.

Cross-Platform Compatibility

Since os.uname isn't available on Windows, this example shows how to write compatible code that works across different platforms.

cross_platform.py
import os
import platform

def get_system_info():
    try:
        uname = os.uname()
        return {
            "system": uname.sysname,
            "node": uname.nodename,
            "release": uname.release,
            "version": uname.version,
            "machine": uname.machine
        }
    except AttributeError:
        return {
            "system": platform.system(),
            "node": platform.node(),
            "release": platform.release(),
            "version": platform.version(),
            "machine": platform.machine()
        }

info = get_system_info()
print("System Information:")
for key, value in info.items():
    print(f"{key:>10}: {value}")

This function first tries os.uname(), falling back to platform module functions if unavailable. The output format remains consistent across platforms.

The platform module provides similar information and works on all platforms.

Custom System Report

This advanced example creates a detailed system report combining os.uname with other system information sources.

system_report.py
import os
import platform
import socket
import multiprocessing
import datetime

def generate_system_report():
    report = {}
    
    # Basic system info
    try:
        uname = os.uname()
        report['os'] = {
            'name': uname.sysname,
            'hostname': uname.nodename,
            'release': uname.release,
            'version': uname.version,
            'architecture': uname.machine
        }
    except AttributeError:
        report['os'] = {
            'name': platform.system(),
            'hostname': platform.node(),
            'release': platform.release(),
            'version': platform.version(),
            'architecture': platform.machine()
        }
    
    # Additional system details
    report['cpu'] = {
        'cores': multiprocessing.cpu_count(),
        'architecture': platform.machine()
    }
    
    report['network'] = {
        'hostname': socket.gethostname(),
        'fqdn': socket.getfqdn()
    }
    
    report['python'] = {
        'version': platform.python_version(),
        'implementation': platform.python_implementation()
    }
    
    report['timestamp'] = datetime.datetime.now().isoformat()
    
    return report

# Generate and display report
report = generate_system_report()
print("COMPREHENSIVE SYSTEM REPORT")
print("=" * 40)
for category, data in report.items():
    print(f"\n{category.upper()}:")
    for key, value in data.items():
        print(f"  {key:15}: {value}")

This script creates a detailed system report combining information from multiple sources. It handles both Unix and Windows systems gracefully.

The report includes OS details, CPU information, network details, and Python environment information.

Security Considerations

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.