How to get CPU Temperature using Python?

There are a few ways to get the CPU temperature using Python. One way is to use the psutil library.

This library provides a cross-platform way to get various system statistics, including the CPU temperature.

To install psutil, you can use the pip command:

pip install psutil

Once psutil is installed, you can use the following code to get the CPU temperature:

import psutil temp = psutil.sensors_temperatures()['cpu_temp'] print(temp)
Code language: JavaScript (javascript)

Another way to get the CPU temperature is to use the os library. This library provides a cross-platform way to interact with the operating system.

To get the CPU temperature, you can use the following code:

import os temp = os.popen('vcgencmd measure_temp').readline() print(temp)
Code language: JavaScript (javascript)

You can also use a third-party library such as wmi (Windows Management Instrumentation) or gtop (Linux). These libraries provide more comprehensive information about the system, but they may be overkill for getting just the CPU temperature.

Fulll code, get CPU Temperature using Python

# importing the psutil library import psutil # Note: It is not for windows user data = psutil.sensors_temperatures() print("Current Temperature of CPU (celcius): ", data['coretemp'][0][1])
Code language: PHP (php)

CPU Temperature

This python script is used to get cpu temperature

  • psutil (process and system utilities) is a cross-platform library.
  • It is used for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python.

Explanation of the script

  • Using psutil library’s inbuilt function “sensors_temperatures()” to measure temperature of CPU

Setup instructions

  • Clone the repo to your machine
  • Head over to the required folder (Cpu Temperature folder)
  • Install psutil using
pip install psutil

Run the script

Code language: CSS (css)

Source code here

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts