Skip to content Skip to sidebar Skip to footer

Get Cpu And Gpu Temp Using Python Windows

I was wondering if there was a way to get the CPU and the GPU temperature in python. I have already found a way for Linux (using psutil.sensors_temperature()), and I wanted to find

Solution 1:

I think there doesn't have a directly way to achieve that. Some CPU producers wouldn't provide wmi to let your know the temperature directly.

You could use OpenHardwareMoniter.dll. Use the dynamic library.

Firstly, Download the OpenHardwareMoniter. It contains a file called OpenHardwareMonitorLib.dll (version 0.9.6, December 2020).

Install the module pythonnet:

pip install pythonnet

Below code works fine on my PC (Get the CPU temperature):

import clr # the pythonnet module.
clr.AddReference(r'YourdllPath') 
# e.g. clr.AddReference(r'OpenHardwareMonitor/OpenHardwareMonitorLib'), without .dllfrom OpenHardwareMonitor.Hardware import Computer

c = Computer()
c.CPUEnabled = True# get the Info about CPU
c.GPUEnabled = True# get the Info about GPU
c.Open()
whileTrue:
    for a inrange(0, len(c.Hardware[0].Sensors)):
        # print(c.Hardware[0].Sensors[a].Identifier)if"/temperature"instr(c.Hardware[0].Sensors[a].Identifier):
            print(c.Hardware[0].Sensors[a].get_Value())
            c.Hardware[0].Update()

To Get the GPU temperature, change the c.Hardware[0] to c.Hardware[1].

Compare the result with :

enter image description here

enter image description here

Attention: If you want to get the CPU temperature, you need to run it as Administrator. If not, you will only get the value of Load. For GPU temperature, it can work without Admin permissions (as on Windows 10 21H1).

I did some changes from a Chinese Blog

Solution 2:

I found a pretty good module for getting the temperature of NVIDIA GPUs.

pip install gputil

Code to print out temperature

import GPUtil
gpu = GPUtil.getGPUs()[0]
print(gpu.temperature)

I found it here

Solution 3:

so you can use gpiozero to get the temp first pip install gpiozero and from gpiozero import CPUTemperature to import it and cpu = CPUTemperature() print(cpu.temperature)

code:

from gpiozero import CPUTemperature

cpu = CPUTemperature()
print(cpu.temperature)

hope you enjoy. :)

Post a Comment for "Get Cpu And Gpu Temp Using Python Windows"