Python is a general-purpose programming language that is relatively simple to learn. Python is often used for web development, data analysis, and more. For this reason, it’s important to be able to gather information about your computer and hardware. The following is a guide to system and hardware information in Python.

Why do we need System and Hardware Information?

There are many reasons why we might need system and hardware information in Python. For example, we might need to:

  • if we wanted to get information about a computer system for troubleshooting purposes, we could use such a tool to help identify the problem.
  •  if we were developing a new application or system, we could use a system information tool to help us understand how the system works and its capabilities.
  • system information tools can be used for system administration tasks, such as managing user accounts or monitoring system performance
  • Check if a particular piece of hardware is present and Monitor environmental conditions like temperature or humidity

This is just a small sampling of the possibilities – system and hardware information can be extremely useful in a wide variety of applications. Python makes it easy to access this information, making it a great choice for working with system and hardware data.

Prerequisites:

To display hardware information in python we will need four different modules. Three of them are going to be external and one of them is the core python module.

1. Psutil: Psutil is a python module that provides an interface for retrieving information on running processes and system utilization (CPU, memory, disks, network) in an easy-to-use manner. It is useful for monitoring system performance and for debugging purposes.

The module is cross-platform and works on Windows, Linux, and OS X. It is written in pure python and has no dependencies. To install psutil use the following command in your command prompt.

pip install psutil

2. cpuinfo: The cpuinfo module in python is a module that allows you to get information about the CPU on your system. This module is designed to work with the Linux kernel, and it provides a number of features that allow you to get information about your CPU. For example, you can use this module to get information about the number of CPUs on your system, the type of CPU, the speed of the CPU, and the amount of memory that the CPU has. To install cpuinfo use the following command in your command prompt.

Pip install py-cpuinfo

3. Wmi: The wmi module is a Python interface to the Windows Management Instrumentation (WMI) framework. WMI is a Microsoft technology that provides system and hardware information to authorized users. The wmi module enables Python scripts to connect to WMI and retrieve information about the system and its hardware. Wmi module only works on windows system. To install wmi use the following command in your command prompt.

Pip install wmi

4. Platform: The platform module in Python provides access to various platform-specific features. For example, it can be used to determine the operating system in use, the processor architecture, and the system locale. Additionally, the platform module can be used to detect whether a given Python interpreter is running in a virtual environment. You don’t need to install the wmi module because it is a core python library.

Code:

Platform module:

Getting the architecture, Network Name, and operating system of the machine using the platform module.

import platform
print(f"Architecture: {platform.architecture()}")
print(f"Network Name: {platform.node()}")
print(f"Operating system: {platform.platform()}")
print(f"Processor: {platform.processor()}")

Output:

cpuinfo module:

Getting CPU information using the cup info module

import cpuinfo
my_cpuinfo = cpuinfo.get_cpu_info()
print(my_cpuinfo)

Output;

As you can see this is a dictionary you can access to get the information for example to get the brand of the CPU we need to use the brand_raw key.

import cpuinfo
my_cpuinfo = cpuinfo.get_cpu_info()
print(f"Full CPU Name: {my_cpuinfo['brand_raw']}")

Output:

Full CPU Name: AMD Ryzen 5 5500U with Radeon Graphics

Similar to this you can access any info from the dictionary using the key 

print(f"CPU architecture: {my_cpuinfo['arch']}")

Output:

CPU architecture: X86_64

Psutil module:

Display total ram of the system:

import psutilprint(f"Total RAM: {psutil.virtual_memory().total / 1024 / 1024 / 1024:.2f} GB")

Output:

Total RAM: 7.36 GB

Display cores and frequency of the CPU

import psutil

#Number of physical cores
print(f"Number of physical cores: {psutil.cpu_count(logical=False)}")
#Number of logical cores
print(f"Number of logical cores: {psutil.cpu_count(logical=True)}")
#Current frequency
print(f"Current CPU frequency: {psutil.cpu_freq().current}")
#Min frequency
print(f"Min CPU frequency: {psutil.cpu_freq().min}")
#Max frequency
print(f"Max CPU frequency: {psutil.cpu_freq().max}")

Output:

Wmi module:

Getting the GPU info

import wmi
pc = wmi.WMI()
print(pc.Win32_VideoController()[0])

Output:

Note: you always have to specify a function name

You can also get processor info and os info in more detail using wmi. 

import wmi
pc = wmi.WMI()
os_info = pc.Win32_operatingSystem()[0]
print(os_info)
print(pc.Win32_Processor()[0])

Output:

You can get only the name of GPU CPU or OS like this.

import wmi
pc = wmi.WMI()

print(pc.Win32_Processor()[0].name)
print(pc.Win32_VideoController()[0].name)

Output:

Check CPU and RAM uses:

import psutil

#CPU utilization
print(f"CPU utilization: {psutil.cpu_percent(interval=1)}")
#CPU utilization of every core
print(f"CPU utilization of every core: {psutil.cpu_percent(interval=1, percpu=True)}")

#Total RAM
print(f"Total RAM installed: {round(psutil.virtual_memory().total/1000000000, 2)} GB")
#Available RAM
print(f"Available RAM: {round(psutil.virtual_memory().available/1000000000, 2)} GB")
#Used RAM
print(f"Used RAM: {round(psutil.virtual_memory().used/1000000000, 2)} GB")
#RAM usage
print(f"RAM usage in %: {psutil.virtual_memory().percent}%")

Output:

Conclusion:

Python provides a wide range of tools for interacting with system and hardware information. This makes it an ideal choice for automating tasks that need to gather this type of data. In this article, we’ve covered some of the most commonly used tools for this purpose.

You can use these tools to build a simple python script that displays basic system info, or you can build a command line application or GUI application around it so you can use it on any system to get quick system and hardware info. If you have any other questions, feel free to contact us.