On this page
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
- Use
sys.platformfor fast OS checks (sys.platform == "darwin",sys.platform.startswith("linux"),sys.platform == "win32"):platform.system()may invokeunameor extra helpers. platform.architecture()inspects the Python binary, not necessarily the OS kernel (e.g., 32-bit Python on 64-bit Windows).freedesktop_os_release()is Linux-only: Guard withsys.platform == "linux"and handleOSError.
Common Functions
- platform.android_ver() —
android_ver(release='', api_level=0, manufacturer='', model='', device='', is_emulator=False)—platform.android_ver()function. - platform.architecture() —
architecture(executable='/opt/homebrew/opt/python@3.14/bin/python3.14', bits='', linkage='')— Queries the given executable (defaults to the Python interpreter binary) for various architecture information. - platform.freedesktop_os_release() —
freedesktop_os_release()— Return operation system identification from freedesktop.org os-release - platform.invalidate_caches() —
invalidate_caches()— Invalidate the cached results. - platform.ios_ver() —
ios_ver(system='', release='', model='', is_simulator=False)— Get iOS version information, and return it as a namedtuple: (system, release, model, is_simulator). - platform.java_ver() —
java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', ''))— Version interface for Jython. - platform.libc_ver() —
libc_ver(executable=None, lib='', version='', chunksize=16384)— Tries to determine the libc version that the file executable (which defaults to the Python interpreter) is linked against. - platform.mac_ver() —
mac_ver(release='', versioninfo=('', '', ''), machine='')— Get macOS version information and return it as tuple (release, versioninfo, machine) with versioninfo being a tuple (version, dev_stage, non_release_version). - platform.machine() —
machine()— Returns the machine type, e.g. 'i386' An empty string is returned if the value cannot be determined. - platform.node() —
node()— Returns the computer's network name (which may not be fully qualified) An empty string is returned if the value cannot be determined. - platform.platform() —
platform(aliased=False, terse=False)— Returns a single string identifying the underlying platform with as much useful information as possible (but no more :). - platform.processor() —
processor()— Returns the (true) processor name, e.g. 'amdk6' An empty string is returned if the value cannot be determined. Note that many platforms do not provide this - platform.python_branch() —
python_branch()— Returns a string identifying the Python implementation branch. - platform.python_build() —
python_build()— Returns a tuple (buildno, builddate) stating the Python build number and date as strings. - platform.python_compiler() —
python_compiler()— Returns a string identifying the compiler used for compiling Python. - platform.python_implementation() —
python_implementation()— Returns a string identifying the Python implementation. - platform.python_revision() —
python_revision()— Returns a string identifying the Python implementation revision. - platform.python_version() —
python_version()— Returns the Python version as string 'major.minor.patchlevel' Note that unlike the Python sys.version, the returned value will always include the patchlevel (it defaults to 0). - platform.python_version_tuple() —
python_version_tuple()— Returns the Python version as tuple (major, minor, patchlevel) of strings. - platform.release() —
release()— Returns the system's release, e.g. '2.2.0' or 'NT' An empty string is returned if the value cannot be determined. - platform.system() —
system()— Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. - platform.system_alias() —
system_alias(system, release, version)— marketing names used for some systems. - platform.uname() —
uname()— Fairly portable uname interface. Returns a tuple of strings (system, node, release, version, machine, processor) identifying the underlying platform. - platform.version() —
version()— Returns the system's release version, e.g. '#3 on degas' An empty string is returned if the value cannot be determined. - platform.win32_edition() —
win32_edition()—platform.win32_edition()function. - platform.win32_is_iot() —
win32_is_iot()—platform.win32_is_iot()function. - platform.win32_ver() —
win32_ver(release='', version='', csd='', ptype='')—platform.win32_ver()function.
Classes & Objects
- platform.AndroidVer — AndroidVer(release, api_level, manufacturer, model, device, is_emulator) (Methods:
.count(),.index()) - platform.IOSVersionInfo — IOSVersionInfo(system, release, model, is_simulator) (Methods:
.count(),.index()) - platform.uname_result — A uname_result that's largely compatible with a simple namedtuple except that 'processor' is resolved late and cached to avoid calling "uname" (Methods:
.count(),.index())
Linked from
Related notes
- tuts/python3/stdlib_module/pkgutil Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/plistlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/getopt Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/io Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/os Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/platform/android_ver Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/platform/AndroidVer Linked here; shared tags: #stdlib