Skip to content

How to Get the OS Name and Version in Python

Learn how to retrieve operating system details in Python using the platform, os, and sys modules for cross-platform scripts, debugging, and compatibility checks.

Knowing the operating system and its version at runtime is essential in many Python applications, from writing cross-platform scripts that behave differently on Windows, Linux, and macOS, to logging system information for debugging, or checking compatibility before running OS-specific commands.

Python provides several built-in modules to retrieve this information. This guide covers each approach with practical examples and explains when to use which method.

Quick Overview

import platform

print(f"System: {platform.system()}")
print(f"Version: {platform.version()}")
print(f"Release: {platform.release()}")

Output (example on Ubuntu Linux):

System: Linux
Version: #78~20.04.1-Ubuntu SMP Wed Oct 9 15:30:47 UTC 2024
Release: 5.15.0-1072-aws

The platform module is the most comprehensive option for retrieving OS information. It provides multiple functions that return different levels of detail.

platform.system() (OS Name)

Returns the OS name as a clean, readable string:

import platform

os_name = platform.system()
print("OS Name:", os_name)

Output varies by OS:

Operating System platform.system() Returns
Windows 'Windows'
Linux 'Linux'
macOS 'Darwin'

platform.version() (OS Version)

Returns the detailed version string of the operating system:

import platform

print("OS Version:", platform.version())

Output examples:

OS Version: 10.0.22631

OS Version: #78~20.04.1-Ubuntu SMP Wed Oct 9 15:30:47 UTC 2024

OS Version: Darwin Kernel Version 23.1.0

platform.release() (OS Release)

Returns the release number, which is often more concise than version():

import platform

print("OS Release:", platform.release())

Output examples:

OS Release: 10

OS Release: 5.15.0-1072-aws

OS Release: 23.1.0

platform.uname() (Complete System Information)

Returns a named tuple with all system details in one call:

import platform

info = platform.uname()

print(f"System:    {info.system}")
print(f"Node:      {info.node}")
print(f"Release:   {info.release}")
print(f"Version:   {info.version}")
print(f"Machine:   {info.machine}")
print(f"Processor: {info.processor}")

Output (example on Linux):

System:    Linux
Node:      my-server
Release:   5.15.0-1072-aws
Version:   #78~20.04.1-Ubuntu SMP Wed Oct 9 15:30:47 UTC 2024
Machine:   x86_64
Processor: x86_64

platform.platform() (Human-Readable Summary)

Returns a single string summarizing the platform:

import platform

print(platform.platform())

Output examples:

Windows-10-10.0.22631-SP0

Linux-5.15.0-1072-aws-x86_64-with-glibc2.31

macOS-14.1-arm64-arm-64bit

Using the os Module

The os module provides a simpler but less detailed way to identify the operating system through the os.name attribute.

import os

print("OS Name:", os.name)

Output varies by OS:

Operating System os.name Returns
Windows 'nt'
Linux / macOS / Unix 'posix'

Using the sys Module

The sys.platform attribute provides a platform identifier string that is more specific than os.name:

import sys

print("Platform:", sys.platform)

Output varies by OS:

Operating System sys.platform Returns
Windows 'win32'
Linux 'linux'
macOS 'darwin'

This is useful for conditional logic based on the operating system:

import sys

if sys.platform == 'win32':
    print("Running on Windows")
elif sys.platform == 'darwin':
    print("Running on macOS")
elif sys.platform == 'linux':
    print("Running on Linux")
else:
    print(f"Running on {sys.platform}")

Practical Example: System Information Report

Here's a complete script that generates a formatted system information report:

import platform
import sys
import os

def get_system_info():
    """Generate a comprehensive system information report."""
    info = {
        "OS Name": platform.system(),
        "OS Release": platform.release(),
        "OS Version": platform.version(),
        "Platform": platform.platform(),
        "Architecture": platform.machine(),
        "Processor": platform.processor() or "N/A",
        "Python Version": platform.python_version(),
        "os.name": os.name,
        "sys.platform": sys.platform,
    }
    return info

report = get_system_info()

print("=" * 50)
print("       SYSTEM INFORMATION REPORT")
print("=" * 50)
for key, value in report.items():
    print(f"  {key:20s}: {value}")
print("=" * 50)

Output (example on Linux):

==================================================
       SYSTEM INFORMATION REPORT
==================================================
  OS Name             : Linux
  OS Release          : 5.15.0-1072-aws
  OS Version          : #78~20.04.1-Ubuntu SMP Wed Oct 9 15:30:47 UTC 2024
  Platform            : Linux-5.15.0-1072-aws-x86_64-with-glibc2.31
  Architecture        : x86_64
  Processor           : x86_64
  Python Version      : 3.10.12
  os.name             : posix
  sys.platform        : linux
==================================================

Comparison of Methods

Method Returns Differentiates Linux/macOS? Detail Level
platform.system() 'Windows', 'Linux', 'Darwin' ✅ OS name only
platform.version() Full version string ✅ Detailed
platform.release() Release number ✅ Moderate
platform.platform() Complete summary string ✅ Most detailed
platform.uname() Named tuple with all info ✅ Comprehensive
os.name 'nt' or 'posix' ❌ Minimal
sys.platform 'win32', 'linux', 'darwin' ✅ Minimal

Conclusion

For comprehensive OS information, the platform module is the best choice: it provides the OS name, version, release, architecture, and more through dedicated functions.

  • Use platform.system() for a clean OS name.
  • Use platform.platform() for a human-readable summary.
  • Use platform.uname() when you need everything in one call.

For simple conditional checks in cross-platform code, sys.platform is concise and effective.

The os.name attribute is the most basic option and is best reserved for distinguishing between Windows and Unix-like systems.

Navigation