System and Hardware Information using Python

In this article we will discuss how to get system and hardware information of your computer using Python.

Table of Contents

System information

CPU usage
Number of physical and logical cores
CPU frequency
CPU utilization percentage

RAM usage


We often look at our system information and open the task manager to see the utilization of our CPU and RAM and look through the processes running.

There is a convenient way of retrieving such information using Python in a few lines of code.

To continue following this tutorial we will need the following two Python libraries: platform and psutil.

The platform module is already installed by default.

If you don’t have psutil installed, please open “Command Prompt” (on Windows) and install it using the following code:

pip install psutil

Get System Information using Python

The platform module in Python allows us to access the underlying platform’s data.

This includes:

Computer network name
Machine type
Processor type
Platform type
Operating system
Operating system release
Operating system version

import platform

#Computer network name
print(f”Computer network name: {platform.node()}”)
#Machine type
print(f”Machine type: {platform.machine()}”)
#Processor type
print(f”Processor type: {platform.processor()}”)
#Platform type
print(f”Platform type: {platform.platform()}”)
#Operating system
print(f”Operating system: {platform.system()}”)
#Operating system release
print(f”Operating system release: {platform.release()}”)
#Operating system version
print(f”Operating system version: {platform.version()}”)

And we get:

Computer network name: DESKTOP-85IO2S6
Machine type: AMD64
Processor type: Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
Platform type: Windows-10-10.0.19041-SP0
Operating system: Windows
Operating system release: 10
Operating system version: 10.0.19041

Get CPU Usage using Python

To make explanations in this section easier, let’s first take a look at the current system information of our computers.

On Windows, you can search for “System Information” in the Search tab and open it. Here we are interested in the information about the processor we have. On my computer I have:

Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz, 2808 Mhz, 6 Core(s), 6 Logical Processor(s)

Python library psutil allows us to get the stats of our CPU by using Python and we will explore how to retrieve the above information. To get started, we should import the library:

import psutil

Get number of physical and logical cores using Python

Let’s find how many physical and logical cores we have. A logical core is the number of physical cores multiplied by the number of threads that can run on each physical core. In my case, each physical core has only one thread, meaning that I have 6 physical cores which translates into 6 logical cores.

We can do this by using the .cpu_count() method which returns a numerical value for the count of cores:

#Physical cores
print(f”Number of physical cores: {psutil.cpu_count(logical=False)}”)
#Logical cores
print(f”Number of logical cores: {psutil.cpu_count(logical=True)}”)

And we get:

Number of physical cores: 6
Number of logical cores: 6

It’s the same information as we retrieved from System Information.

Get CPU frequency using Python

Next let’s retrieve the CPU frequency using Python.

We can do this by using the .cpu_freq() method which returns a named tuple of values (floats) that measure frequency in Mhz: [current frequency, min frequency, max frequency]:

#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}”)

And we get:

Current CPU frequency: 2808.0
Min CPU frequency: 0.0
Max CPU frequency: 2808.0

Looks exactly like what we see in the System Information.

Get CPU utilization percentage using Python

The most interesting information is the utilization of your CPU. This information you should be able to find in the Task Manager. As the processes that run on your computer change, the utilization is dynamic and changes constantly.

As we have 6 cores, we can get the overall CPU utilization (average of 6 cores), as well as the utilization of each CPU using the .cpu_percent() method:

#System-wide CPU utilization
print(f”Current CPU utilization: {psutil.cpu_percent(interval=1)}”)
#System-wide per-CPU utilization
print(f”Current per-CPU utilization: {psutil.cpu_percent(interval=1, percpu=True)}”)

And we get:

Current CPU utilization: 10.7
Current per-CPU utilization: [19.7, 9.4, 12.3, 6.2, 1.6, 1.6]

Note: interval=1 should ideally be set to any value >0 in order to compare the CPU times before and after the interval. You can find more information about it here.

Get RAM Usage using Python

We can also retrieve the RAM usage in Python.

Continuing with similar steps as in the previous sections, we will use the .virtual_memory() method. It returns a named tuple of numerical values that measure memory in bytes and usage in percentage: [total, available, percent used]:

#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: {psutil.virtual_memory().percent}%”)

And we get:

Total RAM installed: 17.05 GB
Available RAM: 6.76 GB
Used RAM: 10.29 GB
RAM usage: 60.3%

Note: my computer has total 16GB RAM installed, and we are not sure why the total RAM installed value is different.


In this tutorial we discussed how to get system and hardware information of your computer using Python.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming articles.

The post System and Hardware Information using Python appeared first on PyShark.

Flatlogic Admin Templates banner

Leave a Reply

Your email address will not be published. Required fields are marked *