Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Access to the underlying platform's identifying data in Python
The platform module in Python provides functions to access information about the underlying system's hardware, operating system, and interpreter version. This is useful for system administration, debugging, and creating platform-specific code.
Basic System Information
architecture()
This function queries the given executable (defaults to the Python interpreter executable) for various architecture information ?
import platform print(platform.architecture())
('64bit', '')
machine()
This function returns the machine type, e.g. 'i386'. An empty string is returned if the value cannot be determined ?
import platform print(platform.machine())
x86_64
system()
This function returns the system/OS name ?
import platform print(platform.system())
Linux
Network and Platform Information
node()
This function returns the computer's network name ?
import platform print(platform.node())
computer-name
platform()
This function returns a single string identifying the underlying platform ?
import platform print(platform.platform())
Linux-5.4.0-74-generic-x86_64-with-glibc2.31
processor()
This function returns the (real) processor name ?
import platform print(platform.processor())
x86_64
Python-Specific Information
python_build()
This function returns a tuple (buildno, builddate) ?
import platform print(platform.python_build())
('default', 'Jun 1 2022 08:30:21')
python_compiler()
This function returns a string identifying the compiler used for compiling Python ?
import platform print(platform.python_compiler())
GCC 9.4.0
python_implementation()
This function returns a string identifying the Python implementation. Possible return values are: 'CPython', 'IronPython', 'Jython', 'PyPy' ?
import platform print(platform.python_implementation())
CPython
python_version()
This function returns a string containing the Python version in the form 'major.minor.patchlevel' ?
import platform print(platform.python_version())
3.9.12
Complete System Information
uname()
Fairly portable uname interface. Returns a namedtuple containing six attributes: system, node, release, version, machine, and processor ?
import platform
result = platform.uname()
print(result)
print(f"System: {result.system}")
print(f"Node: {result.node}")
print(f"Release: {result.release}")
uname_result(system='Linux', node='computer-name', release='5.4.0-74-generic', version='#83-Ubuntu SMP Sat May 8 02:35:39 UTC 2021', machine='x86_64', processor='x86_64') System: Linux Node: computer-name Release: 5.4.0-74-generic
Practical Example
Here's a complete example that gathers all platform information ?
import platform
def get_system_info():
info = {
'Architecture': platform.architecture(),
'Machine': platform.machine(),
'System': platform.system(),
'Platform': platform.platform(),
'Python Version': platform.python_version(),
'Python Implementation': platform.python_implementation()
}
for key, value in info.items():
print(f"{key}: {value}")
get_system_info()
Architecture: ('64bit', '')
Machine: x86_64
System: Linux
Platform: Linux-5.4.0-74-generic-x86_64-with-glibc2.31
Python Version: 3.9.12
Python Implementation: CPython
Conclusion
The platform module provides comprehensive system information for cross-platform compatibility. Use these functions to adapt your code behavior based on the underlying system, operating system, or Python implementation.
