On this page
  1. platform
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Normalizing Architecture Names Across Operating Systems
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects

platform

Prev: pkgutil | Up: stdlib_module | Next: plistlib
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — platform
Tags: #public #python3 #stdlib


Import

import platform

Overview

Queries portable OS, hardware architecture, and Python interpreter metadata (system, release, machine, python_version, python_implementation, mac_ver, freedesktop_os_release). Reach for platform when logging diagnostic environment banners, selecting OS-specific binaries, or gating platform tests.

Quick Start

import platform

info = {
    "os": platform.system(),          # 'Darwin', 'Linux', 'Windows'
    "arch": platform.machine(),       # 'arm64', 'x86_64', 'AMD64'
    "python": platform.python_version(),
    "impl": platform.python_implementation(),
}
assert info["os"] in {"Darwin", "Linux", "Windows"} or isinstance(info["os"], str)

Implementation Walkthrough

1. Normalizing Architecture Names Across Operating Systems

Windows reports "AMD64", Linux reports "x86_64" or "aarch64", and macOS reports "arm64"—normalize once in a small helper:

import platform

def normalized_arch() -> str:
    raw = platform.machine().lower()
    if raw in {"x86_64", "amd64"}:
        return "x86_64"
    if raw in {"arm64", "aarch64"}:
        return "arm64"
    return raw

Gotchas & Boundaries

Common Functions

Classes & Objects

Linked from

Related notes