0
0
PythonHow-ToBeginner · 3 min read

How to Get System Info in Python: Simple Guide

You can get system info in Python using the platform module for details like OS, machine type, and Python version. The os module also provides environment and system-related info.
📐

Syntax

The platform module provides functions like platform.system() to get the OS name, platform.release() for OS version, and platform.machine() for hardware type. The os module offers os.name for the OS family and os.environ for environment variables.

python
import platform
import os

# Get OS name
os_name = platform.system()

# Get OS version
os_version = platform.release()

# Get machine type
machine = platform.machine()

# Get Python version
python_version = platform.python_version()

# Get OS family
os_family = os.name

# Get environment variables
env_vars = os.environ
💻

Example

This example shows how to print key system information like OS, version, machine type, and Python version using the platform module.

python
import platform

print("Operating System:", platform.system())
print("OS Version:", platform.release())
print("Machine Type:", platform.machine())
print("Python Version:", platform.python_version())
Output
Operating System: Linux OS Version: 5.15.0-1051-azure Machine Type: x86_64 Python Version: 3.11.4
⚠️

Common Pitfalls

One common mistake is confusing os.name with platform.system(). os.name returns a simple string like 'posix' or 'nt', which is less descriptive than platform.system() that returns 'Linux', 'Windows', or 'Darwin'. Also, environment variables from os.environ are case-sensitive on some systems.

python
import os
import platform

# Wrong: Using os.name for detailed OS name
print("os.name:", os.name)  # Outputs 'posix' or 'nt'

# Right: Use platform.system() for clear OS name
print("platform.system():", platform.system())
Output
os.name: posix platform.system(): Linux
📊

Quick Reference

Function/AttributeDescription
platform.system()Returns the OS name (e.g., 'Windows', 'Linux')
platform.release()Returns the OS version
platform.machine()Returns the machine type (e.g., 'x86_64')
platform.python_version()Returns the Python version as a string
os.nameReturns OS family name ('posix', 'nt')
os.environMapping object of environment variables

Key Takeaways

Use the platform module for detailed and user-friendly system info.
Avoid relying solely on os.name for OS identification.
Use platform.python_version() to get the current Python version.
Environment variables are accessible via os.environ but watch for case sensitivity.
Test your code on different OSes to ensure compatibility.