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
Get Hardware and System information using the Python Platform Module.
Python is a versatile language that was built as a general-purpose scripting language. Hence a lot of automation tasks, along with scripting, can be done. Getting the system information becomes an important task in many applications such as machine learning, deep learning, etc., where hardware plays a crucial role. Python provides several methods to gather information about the operating system and hardware.
Getting Overall System Configuration
The platform module in Python provides a way to obtain the overall system configuration in a platform-independent manner. So we can run the same methods to get the system configuration without knowing about the platform beforehand. The uname() method allows us to get comprehensive information about the operating system, such as "Windows," "Linux," or "Darwin" (for macOS). The method also returns the release date as well as the version of the system.
Example
import platform
system_info = platform.uname()
print("System Info:", system_info)
print("\nSystem:", system_info.system)
print("Node Name:", system_info.node)
print("Release:", system_info.release)
print("Version:", system_info.version)
print("Machine:", system_info.machine)
System Info: uname_result(system='Linux', node='tutorialspoint', release='5.4.0-74-generic', version='#83-Ubuntu SMP', machine='x86_64') System: Linux Node Name: tutorialspoint Release: 5.4.0-74-generic Version: #83-Ubuntu SMP Machine: x86_64
Obtaining Specific System Information
The platform module provides individual methods to get specific system details. This approach gives you more control over which information you need and how to format it ?
Example
import platform
system_name = platform.system()
system_release = platform.release()
system_architecture = platform.machine()
system_platform = platform.platform()
python_version = platform.python_version()
print("The system details are as follows:")
print("Operating System:", system_name)
print("Release Version:", system_release)
print("System Architecture:", system_architecture)
print("Platform:", system_platform)
print("Python Version:", python_version)
The system details are as follows: Operating System: Linux Release Version: 5.4.0-74-generic System Architecture: x86_64 Platform: Linux-5.4.0-74-generic-x86_64-with-glibc2.31 Python Version: 3.8.10
Get CPU Information
The platform module also allows us to get information about the processor. However, the module cannot give detailed information about the processor, like the clock speed etc. It can only give basic information like the architecture and processor name ?
Example
import platform
def get_cpu_info():
cpu_info = platform.processor()
return cpu_info
cpu = get_cpu_info()
print("CPU Information:")
print("Processor:", cpu if cpu else "Not available")
print("Architecture:", platform.machine())
print("Platform:", platform.platform())
CPU Information: Processor: x86_64 Architecture: x86_64 Platform: Linux-5.4.0-74-generic-x86_64-with-glibc2.31
Complete System Information Function
Here's a comprehensive function that gathers all available system information using the platform module ?
import platform
def get_complete_system_info():
info = {
'System': platform.system(),
'Node Name': platform.node(),
'Release': platform.release(),
'Version': platform.version(),
'Machine': platform.machine(),
'Processor': platform.processor(),
'Platform': platform.platform(),
'Python Version': platform.python_version(),
'Python Implementation': platform.python_implementation()
}
return info
system_details = get_complete_system_info()
print("Complete System Information:")
print("-" * 40)
for key, value in system_details.items():
print(f"{key}: {value}")
Complete System Information: ---------------------------------------- System: Linux Node Name: tutorialspoint Release: 5.4.0-74-generic Version: #83-Ubuntu SMP Machine: x86_64 Processor: x86_64 Platform: Linux-5.4.0-74-generic-x86_64-with-glibc2.31 Python Version: 3.8.10 Python Implementation: CPython
Comparison of Platform Methods
| Method | Returns | Example Output |
|---|---|---|
platform.system() |
Operating system name | Linux, Windows, Darwin |
platform.machine() |
Machine architecture | x86_64, i386, ARM |
platform.processor() |
Processor information | x86_64, Intel64 Family |
platform.platform() |
Complete platform string | Linux-5.4.0-x86_64-with-glibc2.31 |
Conclusion
The platform module provides a cross-platform way to gather system and hardware information in Python. While it offers basic hardware details, more specialized modules like psutil or GPUtil are needed for detailed hardware monitoring and system resource information.
