Extract system information in Python

Pycharm Visual Studio Code

To extract system information in Python, you can use the platform module to get basic information about the system, such as the operating system, architecture, and hardware. You can also use additional libraries like psutil for more detailed information

Here's an example using both platform and psutil:

import platform
import psutil

# Using platform module
system_info = {
    "System": platform.system(),
    "Node Name": platform.node(),
    "Release": platform.release(),
    "Version": platform.version(),
    "Machine": platform.machine(),
    "Processor": platform.processor()
}

print("System Information (Using platform module):")
for key, value in system_info.items():
    print(f"{key}: {value}")

# Using psutil for additional information
cpu_info = {
    "Physical Cores": psutil.cpu_count(logical=False),
    "Total Cores": psutil.cpu_count(logical=True),
    "CPU Frequency (MHz)": psutil.cpu_freq().current,
    "CPU Usage (%)": psutil.cpu_percent(interval=1)
}

print("\nCPU Information (Using psutil):")
for key, value in cpu_info.items():
    print(f"{key}: {value}")

memory_info = {
    "Total Memory (GB)": round(psutil.virtual_memory().total / (1024 ** 3), 2),
    "Available Memory (GB)": round(psutil.virtual_memory().available / (1024 ** 3), 2),
    "Used Memory (GB)": round(psutil.virtual_memory().used / (1024 ** 3), 2),
    "Memory Usage (%)": psutil.virtual_memory().percent
}

print("\nMemory Information (Using psutil):")
for key, value in memory_info.items():
    print(f"{key}: {value}")

The script imports the necessary modules (platform and psutil). It uses platform to retrieve basic system information like operating system, node name, release, version, machine, and processor details. It uses psutil to gather additional information about the CPU and memory, including the number of physical and total cores, CPU frequency, CPU usage percentage, total memory, available memory, used memory, and memory usage percentage. Make sure you have the psutil library installed in your Python environment:

pip install psutil

When you run this script, it will output the system information using both the platform module and psutil library.

Comments
Loading...
Sorry! No comment found:(

There is no comment to show for this.

Leave your comment
Tested Versions
  • Python 3.8