5

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.

8
  • 1
    Might help; stackoverflow.com/q/3262603/6524169
    – Aditya
    Jun 28, 2020 at 5:03
  • I guess you should read more about ACPI Jun 28, 2020 at 5:08
  • I'm always perplexed by this type of question. Why can't you simply use whatever tool is provided by your platform? Why do you feel the need to reinvent the wheel? I love Python and use it everywhere maximizing performance is not an issue. Taking the temperature of the CPU or GPU should not be a performance critical operation. Which means that running an external command to extract that info should be acceptable. Jun 29, 2020 at 4:53
  • I have tried looking for the command. Have you found one? All of the ones I found do not work.
    – KetZoomer
    Jun 30, 2020 at 16:43
  • Read up on psutil.sensors_temperatures(fahrenheit=False)
    – stovfl
    Jul 16, 2020 at 7:51

3 Answers 3

12
+50

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

6
  • Hi, I appreciate the answer. I am fine with downloading the dll and everything, but my main issue with this is that it needs to be ran as admin. I am fine with downloading more things, but is there a way to do this without needing to be run as admin, or is this impossible?
    – KetZoomer
    Jul 16, 2020 at 17:22
  • I think this is the best answer. Just asking though what the compatibility with AMD CPUs.
    – KetZoomer
    Jul 16, 2020 at 23:41
  • @KetZoomer I don't test it with AMD CPU,but I think the OpenHardwareMoniter.dll maybe contains the API about the AMD. Jul 17, 2020 at 1:49
  • @KetZoomer I don't know how to run it without admin,In the blog,the author didn't mention about admin,so I guess maybe it depends on the version of OS.(if you run it on Windows 7, you maybe don't need to run it as admin.) Jul 17, 2020 at 1:54
  • Run as admin saved my code! I got from here: stackoverflow.com/questions/3262603/… and worked all sensors now! Aug 17, 2020 at 23:28
6

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

-2

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. :)

1
  • 1
    I am using windows, not the RPi, and I got this error: gpiozero.exc.BadPinFactory: Unable to load any default pin factory!
    – KetZoomer
    Jun 28, 2020 at 3:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.