platform
The platform module provides functions to access identifying data about the underlying platform where Python is running. This includes the operating system name and version, machine type, processor information, and Python implementation details. It’s useful for conditional code that needs to behave differently across operating systems or gather system information for logging and debugging.
Syntax
import platform
Functions
platform.platform()
Returns a single string identifying the underlying platform with as much useful information as possible.
| Parameter | Type | Default | Description |
|---|---|---|---|
terse | bool | False | If True, return only the minimum information needed to identify the platform |
alias | bool | False | If True, use common marketing names instead of system names (e.g., SunOS becomes Solaris) |
Returns: str — A string identifying the platform
Examples:
# Get full platform information
print(platform.platform())
# Linux-6.12.38-x86_64-with-glibc2.36
# Get minimal platform information
print(platform.platform(terse=True))
# Linux-6.12.38-x86_64
# Get aliased platform name
print(platform.platform(alias=True))
# Solaris
platform.system()
Returns the system/OS name, such as ‘Linux’, ‘Darwin’, ‘Java’, or ‘Windows’.
Returns: str — The operating system name
Examples:
print(platform.system())
# Linux
platform.release()
Returns the system’s release version, e.g., ‘2.2.0’ on Linux or ‘10’ on Windows.
Returns: str — The system release version
Examples:
print(platform.release())
# 6.12.38
platform.version()
Returns the system’s release version with additional details.
Returns: str — The system version string
Examples:
print(platform.version())
# #13 SMP Debian x86_64 GNU/Linux
platform.machine()
Returns the machine type, e.g., ‘AMD64’, ‘x86_64’, ‘arm64’, or ‘aarch64’.
Returns: str — The machine type
Examples:
print(platform.machine())
# x86_64
platform.processor()
Returns the (real) processor name, e.g., ‘Intel(R) Core(TM) i7-9750H’ or ‘AMD Ryzen 7 5800X’.
Returns: str — The processor name, or empty string if unavailable
Examples:
print(platform.processor())
# x86_64
platform.python_version()
Returns the Python version as a string ‘major.minor.patchlevel’.
Returns: str — Python version string
Examples:
print(platform.python_version())
# 3.12.8
# Get tuple version
print(platform.python_version_tuple())
# ('3', '12', '8')
platform.node()
Returns the computer’s network name (may not be fully qualified).
Returns: str — The network node name
Examples:
print(platform.node())
# hostname
Common Patterns
Get all platform info at once
import platform
print(f"System: {platform.system()}")
print(f"Release: {platform.release()}")
print(f"Version: {platform.version()}")
print(f"Machine: {platform.machine()}")
print(f"Processor: {platform.processor()}")
print(f"Python: {platform.python_version()}")
Python implementation details
import platform
print(platform.python_implementation()) # CPython
print(platform.python_compiler()) # GCC 12.2.0
print(platform.python_build()[0]) # main
print(platform.python_branch()) # main
Detect 64-bit vs 32-bit Python
import platform
is_64bit = platform.machine().endswith('64')
print(f"64-bit Python: {is_64bit}")
Cross-platform path handling
import platform
import os
if platform.system() == 'Windows':
config_dir = os.path.join(os.environ['APPDATA'], 'myapp')
else:
config_dir = os.path.expanduser('~/.config/myapp')
os.makedirs(config_dir, exist_ok=True)