Skip to content

How to Get Hardware and System Information in Python

Learn how to retrieve OS, CPU, architecture, and Python version details using Python's built-in platform module with practical examples for cross-platform compatibility checks.

Knowing the details of the system your Python program is running on, such as the operating system, processor, architecture, and Python version, is essential for building cross-platform applications, debugging environment-specific issues, generating system reports, and validating hardware compatibility.

Python's built-in platform module provides a portable way to retrieve detailed information about the hardware and software environment without any third-party dependencies.

In this guide, you'll learn how to use the platform module to access system, hardware, and Python-related information with practical examples.

Getting Started with the platform Module

The platform module is part of Python's standard library, so no installation is required:

import platform

This module works across Windows, macOS, and Linux, returning platform-specific details for each operating system.

Retrieving Basic System Information

The most commonly needed system details can be gathered with a few straightforward function calls:

import platform

print(f"System:       {platform.system()}")
print(f"Node Name:    {platform.node()}")
print(f"Release:      {platform.release()}")
print(f"Version:      {platform.version()}")
print(f"Machine:      {platform.machine()}")
print(f"Processor:    {platform.processor()}")
print(f"Architecture: {platform.architecture()}")
print(f"Platform:     {platform.platform()}")

Example output (Windows):

System:       Windows
Node Name:    DESKTOP-ABC123
Release:      10
Version:      10.0.19041
Machine:      AMD64
Processor:    Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
Architecture: ('64bit', 'WindowsPE')
Platform:     Windows-10-10.0.19041-SP0

Example output (Linux):

System:       Linux
Node Name:    ubuntu-server
Release:      5.15.0-76-generic
Version:      #83-Ubuntu SMP Thu Jun 15 19:16:32 UTC 2023
Machine:      x86_64
Processor:    x86_64
Architecture: ('64bit', 'ELF')
Platform:     Linux-5.15.0-76-generic-x86_64-with-glib2.35

Here's what each function returns:

Function Description
platform.system() Operating system name (e.g., Windows, Linux, Darwin)
platform.node() Network name (hostname) of the machine
platform.release() OS release version
platform.version() Detailed OS version string
platform.machine() Hardware type (e.g., AMD64, x86_64, arm64)
platform.processor() Processor name and model
platform.architecture() Bit architecture and linkage format
platform.platform() Single string summarizing the platform

Retrieving Python Version Information

The platform module also provides details about the Python interpreter itself, which is useful for compatibility checks:

import platform

print(f"Python Version:        {platform.python_version()}")
print(f"Python Version Tuple:  {platform.python_version_tuple()}")
print(f"Python Compiler:       {platform.python_compiler()}")
print(f"Python Build:          {platform.python_build()}")
print(f"Python Implementation: {platform.python_implementation()}")

Example output:

Python Version:        3.11.4
Python Version Tuple:  ('3', '11', '4')
Python Compiler:       MSC v.1934 64 bit (AMD64)
Python Build:          ('main', 'Jun  9 2023 07:30:55')
Python Implementation: CPython

Checking Python Version Compatibility

You can use the version tuple to ensure your script runs on a compatible Python version:

import platform
import sys

version = platform.python_version_tuple()
major, minor = int(version[0]), int(version[1])

if major < 3 or (major == 3 and minor < 8):
    print(f"Python 3.8+ required. You have {platform.python_version()}.")
    sys.exit(1)

print(f"Python {platform.python_version()} - compatible!")

Output:

Python 3.11.4 - compatible!

Collecting All System Information into a Dictionary

For logging, reporting, or debugging purposes, it's helpful to gather all system details into a structured format:

import platform

def get_system_info():
    """Collect comprehensive system information."""
    return {
        "platform": platform.platform(),
        "system": platform.system(),
        "release": platform.release(),
        "version": platform.version(),
        "machine": platform.machine(),
        "processor": platform.processor(),
        "architecture": platform.architecture()[0],
        "hostname": platform.node(),
        "python_version": platform.python_version(),
        "python_compiler": platform.python_compiler(),
        "python_implementation": platform.python_implementation(),
    }


info = get_system_info()

print("System Information Report")
print("=" * 40)
for key, value in info.items():
    print(f"  {key:25s}: {value}")

Example output:

System Information Report
========================================
  platform                 : Windows-10-10.0.19041-SP0
  system                   : Windows
  release                  : 10
  version                  : 10.0.19041
  machine                  : AMD64
  processor                : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
  architecture             : 64bit
  hostname                 : DESKTOP-ABC123
  python_version           : 3.11.4
  python_compiler          : MSC v.1934 64 bit (AMD64)
  python_implementation    : CPython

Getting OS-Specific Information

The platform module provides specialized functions for specific operating systems:

Windows

import platform

if platform.system() == "Windows":
    win_ver = platform.win32_ver()
    print(f"Windows Release:  {win_ver[0]}")
    print(f"Windows Version:  {win_ver[1]}")
    print(f"Service Pack:     {win_ver[2]}")
    print(f"OS Type:          {win_ver[3]}")

Example output:

Windows Release:  10
Windows Version:  10.0.19041
Service Pack:     SP0
OS Type:          Multiprocessor Free

macOS

import platform

if platform.system() == "Darwin":
    mac_ver = platform.mac_ver()
    print(f"macOS Version:  {mac_ver[0]}")
    print(f"Dev Tools Info: {mac_ver[1]}")
    print(f"Machine Type:   {mac_ver[2]}")

Linux

import platform

if platform.system() == "Linux":
    print(f"Linux Distribution: {platform.freedesktop_os_release().get('NAME', 'Unknown')}")

Extended System Info with os and psutil

The platform module covers system identification well, but for hardware metrics like CPU count, memory, and disk usage, combine it with os and the third-party psutil library:

import platform
import os

print(f"CPU Cores: {os.cpu_count()}")

try:
    import psutil

    mem = psutil.virtual_memory()
    print(f"Total RAM: {mem.total / (1024**3):.1f} GB")
    print(f"Available RAM: {mem.available / (1024**3):.1f} GB")
    print(f"CPU Usage: {psutil.cpu_percent(interval=1)}%")
except ImportError:
    print("Install psutil for detailed hardware metrics: pip install psutil")

Example output:

CPU Cores: 8
Total RAM: 16.0 GB
Available RAM: 9.3 GB
CPU Usage: 12.5%

Creating a Complete System Report Function

Here's a production-ready function that generates a comprehensive system report:

import platform
import os
from datetime import datetime

def generate_system_report():
    """Generate a formatted system information report."""
    report = {
        "Report Generated": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "Hostname": platform.node(),
        "OS": platform.system(),
        "OS Release": platform.release(),
        "OS Version": platform.version(),
        "Architecture": platform.architecture()[0],
        "Machine Type": platform.machine(),
        "Processor": platform.processor(),
        "CPU Cores": os.cpu_count(),
        "Python Version": platform.python_version(),
        "Python Implementation": platform.python_implementation(),
        "Python Compiler": platform.python_compiler(),
    }

    if platform.system() == "Windows":
        win = platform.win32_ver()
        report["Windows Edition"] = win[0]
    elif platform.system() == "Darwin":
        report["macOS Version"] = platform.mac_ver()[0]

    return report


report = generate_system_report()

print("\n📋 System Report")
print("=" * 50)
for key, value in report.items():
    print(f"  {key:25s}: {value}")

Example output:

📋 System Report
==================================================
  Report Generated         : 2025-01-15 14:30:22
  Hostname                 : DESKTOP-ABC123
  OS                       : Windows
  OS Release               : 10
  OS Version               : 10.0.19041
  Architecture             : 64bit
  Machine Type             : AMD64
  Processor                : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
  CPU Cores                : 8
  Python Version           : 3.11.4
  Python Implementation    : CPython
  Python Compiler          : MSC v.1934 64 bit (AMD64)
  Windows Edition          : 10

Quick Reference: platform Module Functions

Function Returns
platform.system() OS name (Windows, Linux, Darwin)
platform.node() Hostname
platform.release() OS release number
platform.version() Detailed OS version
platform.machine() Hardware type (AMD64, x86_64)
platform.processor() Processor name and model
platform.architecture() Bit width and linkage (64bit, ELF)
platform.platform() Full platform summary string
platform.python_version() Python version string
platform.python_version_tuple() Python version as (major, minor, patch)
platform.python_compiler() Compiler used to build Python
platform.python_build() Build number and date
platform.python_implementation() Implementation (CPython, PyPy)
platform.uname() Named tuple with all system attributes

Conclusion

Python's platform module is a powerful built-in tool for retrieving hardware and system information without any third-party dependencies:

  • Use platform.system() and platform.machine() for basic OS and hardware identification.
  • Use platform.python_version() to verify Python compatibility at runtime.
  • Use platform.platform() for a single comprehensive platform summary string.
  • Combine with os.cpu_count() and psutil for detailed hardware metrics like RAM and CPU usage.
  • Use OS-specific functions like platform.win32_ver() or platform.mac_ver() for targeted details.

All functions work cross-platform, returning appropriate values for Windows, macOS, and Linux environments.

Navigation