4

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.

KetZoomer
  • 2,701
  • 3
  • 15
  • 43
  • 1
    Might help; https://stackoverflow.com/q/3262603/6524169 – Aditya Jun 28 '20 at 05:03
  • I guess you should read more about [ACPI](https://en.wikipedia.org/wiki/Advanced_Configuration_and_Power_Interface) – Basile Starynkevitch Jun 28 '20 at 05: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. – Kurtis Rader Jun 29 '20 at 04:53
  • I have tried looking for the command. Have you found one? All of the ones I found do not work. – KetZoomer Jun 30 '20 at 16:43
  • Read up on [`psutil.sensors_temperatures(fahrenheit=False)`](https://psutil.readthedocs.io/en/latest/#psutil.sensors_temperatures) – stovfl Jul 16 '20 at 07:51
  • psutil.sensors_temperatures(fahrentheit=False) only works on linux. – KetZoomer Jul 16 '20 at 17:11
  • Editing Questions to improve them (e.g. clarification, adding additional information, etc.) *is encouraged*. However, editing a Question to change it into a different question, which invalidates one or more answers, is against policy on Stack Overflow. Your edit here did so. The policy is that other users with edit privileges should proactively revert such changes. I have reverted your edit. You *are encouraged to [ask a new Question](/questions/ask)*, perhaps with a link to this one for additional context. We want to help, but your new/additional issue needs to be a new Question. – Makyen Nov 09 '20 at 03:12
  • Does this answer your question? [Accessing CPU temperature in python](https://stackoverflow.com/questions/3262603/accessing-cpu-temperature-in-python) – eadmaster Jul 19 '23 at 23:38

3 Answers3

12

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

eco-model
  • 13
  • 2
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • 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 '20 at 17:22
  • I think this is the best answer. Just asking though what the compatibility with AMD CPUs. – KetZoomer Jul 16 '20 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`. – jizhihaoSAMA Jul 17 '20 at 01: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.) – jizhihaoSAMA Jul 17 '20 at 01:54
  • Run as admin saved my code! I got from here: https://stackoverflow.com/questions/3262603/accessing-cpu-temperature-in-python and worked all sensors now! – Marco Antonio Yamada Aug 17 '20 at 23:28
  • Is this answer still valid? Seems like openhw monitor is the ideal. Solution but not updated since 2020? – ScipioAfricanus Dec 25 '22 at 21:19
4

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

GesChen
  • 83
  • 1
  • 7
-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. :)

Jas0n
  • 91
  • 1
  • 2
  • 10
  • 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 '20 at 03:18