6

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 Commented Jun 28, 2020 at 5:03
  • I guess you should read more about ACPI Commented 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. Commented 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. Commented Jun 30, 2020 at 16:43
  • 1
    psutil.sensors_temperatures(fahrentheit=False) only works on linux. Commented Jul 16, 2020 at 17:11

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

Sign up to request clarification or add additional context in comments.

8 Comments

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?
I think this is the best answer. Just asking though what the compatibility with AMD CPUs.
@KetZoomer I don't test it with AMD CPU,but I think the OpenHardwareMoniter.dll maybe contains the API about the AMD.
@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.)
Run as admin saved my code! I got from here: stackoverflow.com/questions/3262603/… and worked all sensors now!
|
10

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

Comments

-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 Comment

I am using windows, not the RPi, and I got this error: gpiozero.exc.BadPinFactory: Unable to load any default pin factory!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.