Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 a way for Windows.

A way to find the temperatures for Mac OS would also be appreciated, but I mainly want a way for windows.

I prefer to only use python modules, but DLL and C/C++ extensions are also completely acceptable!

When I try doing the below, I get None:

import wmi
w = wmi.WMI()
prin(w.Win32_TemperatureProbe()[0].CurrentReading)

When I try doing the below, I get an error:

import wmi
w = wmi.WMI(namespace="root\wmi")
temperature_info = w.MSAcpi_ThermalZoneTemperature()[0]
print(temperature_info.CurrentTemperature)

Error:

wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147217396, 'OLE error 0x8004100c', None, None)>

I have heard of OpenHardwareMoniter, but this requires me to install something that is not a python module. I would also prefer to not have to run the script as admin to get the results.

I am also fine with running windows cmd commands with python, but I have not found one that returns the CPU temp.

Update: I found this: https://stackoverflow.com/a/58924992/13710015. I can't figure out how to use it though. When I tried doing: print(OUTPUT_temp._fields_), I got

[('Board Temp', <class 'ctypes.c_ulong'>), ('CPU Temp', <class 'ctypes.c_ulong'>), ('Board Temp2', <class 'ctypes.c_ulong'>), ('temp4', <class 'ctypes.c_ulong'>), ('temp5', <class 'ctypes.c_ulong'>)]

Note: I really do not want to run this as admin. If I absolutely have to, I can, but I prefer not to.

like image 684
KetZoomer Avatar asked Jul 14 '26 04:07

KetZoomer


1 Answers

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 .dll

from OpenHardwareMonitor.Hardware import Computer

c = Computer()
c.CPUEnabled = True # get the Info about CPU
c.GPUEnabled = True # get the Info about GPU
c.Open()
while True:
    for a in range(0, len(c.Hardware[0].Sensors)):
        # print(c.Hardware[0].Sensors[a].Identifier)
        if "/temperature" in str(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

like image 123
jizhihaoSAMA Avatar answered Jul 15 '26 21:07

jizhihaoSAMA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!