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. System method allows us to get 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)

Output

System Info: uname_result(system='Linux', node='asifr-Nitro-AN515-45', release='5.19.0-43-generic', version='#44~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon May 22 13:39:36 UTC 2', machine='x86_64')

Obtaining System information

The platform is the operating system that we use to run all our applications. This can be Linux, Windows, Mac OS, etc. We can use the platform library to get the name, release date, machine, etc. Such information is helpful to get the system information and its capabilities. This is crucial in fields such as machine learning, where hardware is insignificant in accelerating the process.

Example

import platform
system_name = platform.system()
system_release = platform.release()
system_architecture = platform.machine()
system_aliases = platform.platform()
print("The system details are as follows:")
print("Operating System:", system_name)
print("Release Version:", system_release)
print("System Architecture:", system_architecture)
print("Systemaliases:", system_aliases)

Output

The system details are as follows:
Operating System: Linux
Release Version: 5.19.0-43-generic
System Architecture: x86_64
Systemaliases: Linux-5.19.0-43-generic-x86_64-with-glibc2.35

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 information like the architecture of the design.

Example

In the following code, we first imported the platform module. Next, we created the function get_cpu_info. The function uses the process method of the platform module to get the CPU information and returns it. Next, we called the function and printed the result.

import platform
def get_cpu_info():
    cpu_info = platform.processor()
    return cpu_info
cpu = get_cpu_info()
print("CPU Information:")
print("Processor:", cpu)
print("Architecture:", platform.machine())

Output

CPU Information:
Processor: x86_64
Architecture: x86_64

Conclusion

In this article, we have understood how to use the libraries of Python to get the system and hardware information. Python is a general−purpose scripting language that offers various ways to interact with hardware and software. Multiple libraries are available in Python, like psutil, GPUtil, etc., to get the system and hardware information.

Updated on: 18-Jul-2023

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements