Platform Module in Python
Platform module in Python is a built-in library that provides a portable way to access detailed information about the underlying platform (hardware and operating system) on which your Python program is running. This can include data such as the OS name and version, machine type, processor info and Python runtime details. Understanding the platform your code is running on is useful for:
- Ensuring compatibility with certain Python versions or system architectures.
- Tailoring functionality based on operating system specifics.
- Gathering system diagnostics or environment info for debugging or deployment.
Since it’s part of Python’s standard library, no additional installation is required. You simply import it:
import platform
Platform functions
These functions provide information about the operating system, processor, node and general system details. They're useful when writing code that needs to behave differently depending on the environment.
1. platform.processor(): Returns the processor name or identifier of your system. Helpful when you want to know what CPU the program is running on.
import platform
print(platform.processor())
Output
Intel64 Family 6 Model 154 Stepping 4, GenuineIntel
2. platform.architecture(): Tells whether your Python is running in 32-bit or 64-bit mode. Useful for compatibility checks.
import platform
print(platform.architecture())
Output
('64bit', 'WindowsPE')
3. platform.machine(): Returns the machine type (e.g., ‘x86_64’, ‘AMD64’, etc.). It's handy for identifying the hardware architecture.
import platform
print(platform.machine())
Output
AMD64
4. platform.node(): Gives the network name (hostname) of the computer. Good for identifying the device in networks.
import platform
print(platform.node())
Output
GFG0578-VISHAKSHI
5. platform.platform(): Returns a single string describing the platform (OS name and version). Great for logging system info.
import platform
print(platform.platform())
Output
Windows-10-10.0.26100-SP0
6. platform.system(): Returns the name of the operating system. It’s commonly used for OS-based decisions in scripts.
import platform
print(platform.system())
Output
Windows
7. platform.uname(): Returns a named tuple with detailed system information: system, node, release, version, machine. More detailed than other functions.
import platform
uname = platform.uname()
print(uname)
Output
uname_result(system='Windows', node='GFG0578-VISHAKSHI', release='10', version='10.0.26100', machine='AMD64')
Python runtime information
These functions give details about the currently running Python interpreter, its version, build, compiler and implementation. Useful when ensuring compatibility or diagnosing environment issues.
1. platform.python_version(): Returns the Python version as a string. Useful for ensuring compatibility with your code.
import platform
print(platform.python_version())
Output
3.11.9
2. platform.python_build(): Shows when and how the Python interpreter was built. Useful for debugging or understanding build history.
import platform
print(platform.python_build())
Output
('tags/v3.11.9:de54cf5', 'Apr 2 2024 10:12:12')
3. platform.python_compiler(): Tells which compiler was used to build Python (e.g., MSC for Windows). Helpful for advanced debugging.
import platform
print(platform.python_compiler())
Output
MSC v.1938 64 bit (AMD64)
4. platform.python_implementation(): Returns the Python implementation in use like CPython, PyPy, or Jython.
import platform
print(platform.python_implementation())
Output
CPython
5. platform.python_branch(): Shows the branch from which Python was built. Mostly relevant for developers working with Python source.
import platform
print(platform.python_branch())
Output
tags/v3.11.9
OS specific functions
These platform-specific methods provide details relevant to particular operating systems. They're useful for tailoring behavior to macOS, Windows, or Linux environments.
1. For macOs: platform.mac_ver() gives macOS version details. Only relevant when running the script on a Mac.
print(platform.mac_ver())
Output
('10.15.7', ('', '', ''), 'x86_64')
2. For unix/Linux: platform.libc_ver() returns the version of the C library (glibc) used by the OS. Useful for deep Linux debugging.
print(platform.libc_ver())
Output
('glibc', '2.31')
3. For windows: platform.win32_ver() gives detailed Windows version info. Use it to fine-tune compatibility on different Windows editions.
print(platform.win32_ver())
Output
('10', '10.0.19041', 'SP0', 'Multiprocessor Free')
Related Articles