views:

198

answers:

2

I need an example code for accessing CPU temperature in python.

I'm running windows 7, BTW.

+1  A: 

Check out the cputemp library.

EDIT: on windows, you might be able to convert this IronPython script which uses WMI using the python WMI library.

ars
Doesn't seem to support Windows OSes, though.
Tim Pietzcker
It tells me my hardware isn't supported :/
Anteater7171
Then python is probably not a good language choice for your needs.
Andrei Ciobanu
See if the script in my update helps you.
ars
+1  A: 

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 wmi
w = 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.

Heap
I get the following error with the first code:Traceback (most recent call last): File "<string>", line 244, in run_nodebug File "<module1>", line 3, in <module>IndexError: list index out of range
Anteater7171