Accessing CPU temperature in python Accessing CPU temperature in python python python

Accessing CPU temperature in python


Use the WMI module + Open Hardware Monitor + its WMI interface described here.

Sample code:

import wmiw = wmi.WMI(namespace="root\OpenHardwareMonitor")temperature_infos = w.Sensor()for sensor in temperature_infos:    if sensor.SensorType==u'Temperature':        print(sensor.Name)        print(sensor.Value)


Download http://openhardwaremonitor.org/downloads/ and http://www.cputhermometer.com/ and extract OpenHardwareMonitorLib.dll and CPUThermometerLib.dll and place these in a directory.

You can then use the pythonnet module to address the .dlls and pull any stat that these programs offer. cputhermometer offers per-core CPU temps, openhardwaremonitor offers everything else. No need to use WMI which requires the program to be active in the background.

I have written a small script (python 3.6.5) to show every temperature sensor available on the system, you can of course easily modify this for other sensor types. You must run this as administrator:

import clr #package pythonnet, not clropenhardwaremonitor_hwtypes = ['Mainboard','SuperIO','CPU','RAM','GpuNvidia','GpuAti','TBalancer','Heatmaster','HDD']cputhermometer_hwtypes = ['Mainboard','SuperIO','CPU','GpuNvidia','GpuAti','TBalancer','Heatmaster','HDD']openhardwaremonitor_sensortypes = ['Voltage','Clock','Temperature','Load','Fan','Flow','Control','Level','Factor','Power','Data','SmallData']cputhermometer_sensortypes = ['Voltage','Clock','Temperature','Load','Fan','Flow','Control','Level']def initialize_openhardwaremonitor():    file = 'OpenHardwareMonitorLib.dll'    clr.AddReference(file)    from OpenHardwareMonitor import Hardware    handle = Hardware.Computer()    handle.MainboardEnabled = True    handle.CPUEnabled = True    handle.RAMEnabled = True    handle.GPUEnabled = True    handle.HDDEnabled = True    handle.Open()    return handledef initialize_cputhermometer():    file = 'CPUThermometerLib.dll'    clr.AddReference(file)    from CPUThermometer import Hardware    handle = Hardware.Computer()    handle.CPUEnabled = True    handle.Open()    return handledef fetch_stats(handle):    for i in handle.Hardware:        i.Update()        for sensor in i.Sensors:            parse_sensor(sensor)        for j in i.SubHardware:            j.Update()            for subsensor in j.Sensors:                parse_sensor(subsensor)def parse_sensor(sensor):        if sensor.Value is not None:            if type(sensor).__module__ == 'CPUThermometer.Hardware':                sensortypes = cputhermometer_sensortypes                hardwaretypes = cputhermometer_hwtypes            elif type(sensor).__module__ == 'OpenHardwareMonitor.Hardware':                sensortypes = openhardwaremonitor_sensortypes                hardwaretypes = openhardwaremonitor_hwtypes            else:                return            if sensor.SensorType == sensortypes.index('Temperature'):                print(u"%s %s Temperature Sensor #%i %s - %s\u00B0C" % (hardwaretypes[sensor.Hardware.HardwareType], sensor.Hardware.Name, sensor.Index, sensor.Name, sensor.Value))if __name__ == "__main__":    print("OpenHardwareMonitor:")    HardwareHandle = initialize_openhardwaremonitor()    fetch_stats(HardwareHandle)    print("\nCPUMonitor:")    CPUHandle = initialize_cputhermometer()    fetch_stats(CPUHandle)

Here is the output on my system:

OpenHardwareMonitor:SuperIO Nuvoton NCT6791D Temperature Sensor #0 CPU Core - 42.0°CSuperIO Nuvoton NCT6791D Temperature Sensor #1 Temperature #1 - 35.0°CSuperIO Nuvoton NCT6791D Temperature Sensor #2 Temperature #2 - 34.0°CSuperIO Nuvoton NCT6791D Temperature Sensor #3 Temperature #3 - 25.0°CSuperIO Nuvoton NCT6791D Temperature Sensor #4 Temperature #4 - 101.0°CSuperIO Nuvoton NCT6791D Temperature Sensor #5 Temperature #5 - 16.0°CSuperIO Nuvoton NCT6791D Temperature Sensor #6 Temperature #6 - 14.0°CGpuNvidia NVIDIA GeForce GTX 1070 Temperature Sensor #0 GPU Core - 60.0°CHDD ST31000528AS Temperature Sensor #0 Temperature - 37.0°CHDD WDC WD20EARX-00PASB0 Temperature Sensor #0 Temperature - 36.0°CHDD WDC WDS100T2B0B-00YS70 Temperature Sensor #0 Temperature - 40.0°CHDD WDC WD80EFZX-68UW8N0 Temperature Sensor #0 Temperature - 31.0°CHDD WDC WD30EFRX-68EUZN0 Temperature Sensor #0 Temperature - 30.0°CHDD WDC WD80EFZX-68UW8N0 Temperature Sensor #0 Temperature - 33.0°CHDD Crucial_CT256MX100SSD1 Temperature Sensor #0 Temperature - 40.0°CCPUMonitor:CPU Intel Core i7-8700K Temperature Sensor #0 CPU Core #1 - 39.0°CCPU Intel Core i7-8700K Temperature Sensor #1 CPU Core #2 - 38.0°CCPU Intel Core i7-8700K Temperature Sensor #2 CPU Core #3 - 37.0°CCPU Intel Core i7-8700K Temperature Sensor #3 CPU Core #4 - 41.0°CCPU Intel Core i7-8700K Temperature Sensor #4 CPU Core #5 - 36.0°CCPU Intel Core i7-8700K Temperature Sensor #5 CPU Core #6 - 47.0°C

For further documentation (however you should be able to infer everything you need from the above code), refer to the https://github.com/openhardwaremonitor/openhardwaremonitor/ (or cputhermometer, on the website) source code, the functions and methods are identical when you use these with python.

I haven't tested this on any other computers, so different processor architectures may not function identically.

Ensure you run Hardware[x].Update() between taking measurements (and SubHardware[x].Update() if needed).


You can use pywin32 to access the native Windows API. I believe it should be possible to query the Windows API for the CPU temperature if the manufacturer for your mainboard driver registers a WMI Data Provider through their driver. Assuming this is the case you could download the pywin32 extensions and the Python WMI module mentioned in the answer by ars, and then proceed as follows:

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

Looking at the IronPython script in the ars' answer there seems to be another way to do it too, using a different WMI object. Using the same API and approach you could try receiving the temperature value with

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

which apparently should return the temperature value in tenths of Kelvin, thus to receive the degree in Celsius I guess you just divide this value by 10 and subtract ~273.