platform
The Python platform
module provides access to underlying platform-specific data, such as the operating system (OS), interpreter, and hardware architecture. It’s useful for retrieving information about the environment in which a Python program is running.
Here’s a quick example:
>>> import platform
>>> platform.system()
'Darwin'
Key Features
- Retrieves the OS name, release, and version
- Provides the Python version and build number
- Identifies the processor architecture
- Detects the underlying platform details
Frequently Used Classes and Functions
Object | Type | Description |
---|---|---|
platform.system() |
Function | Returns the system (OS) name |
platform.release() |
Function | Returns the system’s release |
platform.version() |
Function | Returns the system’s version |
platform.machine() |
Function | Returns the machine type |
platform.processor() |
Function | Returns the processor name |
platform.platform() |
Function | Returns a single string identifying the platform |
platform.python_version() |
Function | Returns the Python version as a string |
Examples
Retrieve the current machine type:
>>> platform.machine()
'arm64'
Get the Python version:
>>> platform.python_version()
'3.13.2'
Common Use Cases
- Determining the operating system to adjust functionality accordingly
- Logging environment details for debugging or reporting
- Checking compatibility with system-specific features
Real-World Example
Suppose you need to log system information for troubleshooting purposes. Here’s how you can gather and print this information:
>>> import platform
>>> system_info = {
... "OS": platform.system(),
... "Release": platform.release(),
... "Version": platform.version(),
... "Machine": platform.machine(),
... "Processor": platform.processor(),
... "Python Version": platform.python_version()
... }
>>> for key, value in system_info.items():
... print(f"{key}: {value}")
...
OS: Darwin
Release: 24.5.0
Version: Darwin Kernel Version 24.5.0...
Machine: arm64
Processor: arm
Python Version: 3.13.2
In this example, you use the platform
module to collect and display comprehensive system information, which can be invaluable for debugging and reporting.
Related Resources
Tutorial
The Python Standard REPL: Try Out Code and Ideas Quickly
In this tutorial, you'll learn how to use the Python standard REPL (Read-Eval-Print Loop) to run your code interactively. This tool will allow you to test new ideas, explore and experiment with new tools and libraries, refactor and debug your code, try out examples, and more.
For additional information on related topics, take a look at the following resources:
- Logging in Python (Tutorial)
- How to Debug Common Python Errors (Tutorial)
- Getting the Most Out of the Python Standard REPL (Course)
- The Python Standard REPL: Try Out Code and Ideas Quickly (Quiz)
- Logging Inside Python (Course)
- Logging in Python (Quiz)
- How to Debug Common Python Errors (Quiz)
By Leodanis Pozo Ramos • Updated July 16, 2025