Platform module is a built-in library that provides a portable way to access information about underlying platform (hardware and operating system) on which your Python program is running.
Since it is part of Python’s library, no additional installation is required. User can simply import it as:
import platform
Why do we need Platform Module?
- Provides detailed information about system’s OS, hardware and Python version.
- Useful for writing cross-platform compatible code.
- Helps detect the underlying platform (Windows, Linux, macOS, etc.) at runtime.
- Allows conditional execution based on system architecture or OS type.
- Essential for debugging, logging and system-specific configurations in scripts.
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')