How to Get System Information in Python

In today’s digital world, customized solution as essential for smooth operation. These solutions can be customized as per operating system, memory, storage and many other things. Sometimes, your application need some specific tech requirements perform some tasks and if your user doesn’t have required specification then it can cause issue in performance or even worst your application does not loads. To fix this type of issue, you need to get system information in python or any other programming language to check whether client’s system is capable or not.

The values can be anything as per your requirement like CPU, RAM, disk space or even operating system. This guide will provide you necessary information about how to get system information in Python using the psutil module. We’ll get CPU, RAM, disk, and OS information in a simple and practical way. In these examples, we will use platform and psutil modules.

Installing the psutil Module

To get started, you need to install the psutil module. The psutil a powerful Python library that helps you access system details such as CPU, memory, and disk usage.

For installation, Open your terminal or command prompt and run the following command:

pip install psutil

Once installation is completes, you can start using this module by importing it.

Get System Information in Python

For getting system information like name, OS version and processor you don’t have to use this module. Python has in-built module called platform. However, The psutil provide extended features. Let’s get some basic system information by example:

import psutil
import platform
from datetime import datetime

print("System Information")
print(f"System: {platform.system()}")
print(f"Node Name: {platform.node()}")
print(f"Release: {platform.release()}")
print(f"Version: {platform.version()}")
print(f"Machine: {platform.machine()}")
print(f"Processor: {platform.processor()}")
print(f"Boot Time: {datetime.fromtimestamp(psutil.boot_time())}")

When you run this script, it displays your system name, OS version, machine type, processor details, and the last system startup time. These details help you analyze performance or identify bugs more easily.

Get CPU Information in Python

While working on process heavy features or multi-threading environment like scraping, knowing CPU details useful. The CPU plays a vital role in performance, optimization, monitoring and debugging.

Below is simple example for you to get CPU information in Python using psutil:

import psutil

print("CPU Information")
print(f"Physical cores: {psutil.cpu_count(logical=False)}")
print(f"Total cores: {psutil.cpu_count(logical=True)}")
print(f"CPU Frequency: {psutil.cpu_freq().current} MHz")
print(f"CPU Usage: {psutil.cpu_percent(interval=1)}%")

As output of this script, you will see core and CPU information which comes handy in running multiple processes at a time.

Get RAM Information in Python

Memory or RAM determines how many programs your system can run at once. Tracking memory usage helps to work as real time memory monitor system and help to detect bottlenecks and optimize system performance.

import psutil

print("RAM Information")
memory = psutil.virtual_memory()
print(f"Total: {memory.total / (1024 ** 3):.2f} GB")
print(f"Available: {memory.available / (1024 ** 3):.2f} GB")
print(f"Used: {memory.used / (1024 ** 3):.2f} GB")
print(f"Percentage: {memory.percent}%\n")

print("Swap Memory Information")
swap = psutil.swap_memory()
print(f"Total: {swap.total / (1024 ** 3):.2f} GB")
print(f"Used: {swap.used / (1024 ** 3):.2f} GB")
print(f"Free: {swap.free / (1024 ** 3):.2f} GB")
print(f"Percentage: {swap.percent}%")

Get Disk Information in Python

Disk storage is another critical part of system performance. Monitoring available and used disk space can prevent issues like insufficient storage or application crashes which helps into long runs.

Below is simple example to get disk information in python using psutil module:

import psutil

print("Disk Information")
partitions = psutil.disk_partitions()

for partition in partitions:
    print(f"Device: {partition.device}")
    try:
        usage = psutil.disk_usage(partition.mountpoint)
        print(f"  Total: {usage.total / (1024 ** 3):.2f} GB")
        print(f"  Used: {usage.used / (1024 ** 3):.2f} GB")
        print(f"  Free: {usage.free / (1024 ** 3):.2f} GB")
        print(f"  Percentage: {usage.percent}%\n")
    except PermissionError:
        continue

As result of this script, it will show total size, used size, free space and percentage of usage as per partition.

Conclusion

Python provides simple and efficient way to get system information using the psutil library. You can check CPU load, monitor memory usage, and analyze disk space and operating system details with just a few lines of Python code.

If you’re interested in exploring more Python capabilities, don’t miss our detailed guide on Get Geolocation in Python. It walks you through how to fetch a device’s location using Python in simple and practical ways.