Accessing CPU Temperature in Python

Get CPU and GPU Temp using Python Windows

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 :

Sample Image

Sample Image

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

CPU temperature real time with python default libraries

I'm not sure that such kind of function or module is standard.

You can install the package psutil to use this function (pip install --user psutil):

import psutil

psutil.sensors_temperatures()

Otherwise,if you really don't want to install non standard packages, you might find information somwhere on your computer, for example on Linux:

$ cat /sys/class/thermal/thermal_zone*/temp

You can read those kind of files directly in your Python code but I don't think that's a good practice as it's not cross platform !

How can I get CPU temperature in Python? Assuming that I have Windows 32-bits

Sir you can use simple code for find the CPU temperature using python. And it is not necessary that you should use only 'psutil' for finding the temperature.
You can usethe WMI module + Open Hardware Monitor + its WMI interface described here.

import wmi
w = 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)

# This is a simple code to find the temperature of the CPU using Python.

Get CPU and GPU Temp Using Python WITHOUT ADMIN ACCESS - Windows

Problem

An unprivileged user needs access to functionality only available by a privileged user in a secure manner.

Solution

Create an server-client interface where functionality is decoupled from the actual system as to prevent security issues (ie: don't just pipe commands or options directly from client for execution by the server).

Consider using gRPC for this server-client interface. If you haven't used gRPC before, here's an example of what this entails:

Create a temperature.proto:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "temperature";
option java_outer_classname = "TemperatureProto";
option objc_class_prefix = "TEMP";

package temperature;

service SystemTemperature {
rpc GetTemperature (TemperatureRequest) returns (TemperatureReply) {}
}

message TemperatureRequest {
string name = 1;
}

message TemperatureReply {
string message = 1;
}

Compile the aforementioned with protoc from protobuf library.

python -m grpc_tools.protoc --proto_path=. temperature.proto --python_out=. --grpc_python_out=.

This will generate a file named temperature_pb2_grpc.py, which is where you'll define functionality and response for GetTemperature, note, that you can implement logic branches contextual upon TemperatureRequest options passed from the client.

Once complete simply write and run a temperature_server.py from your privileged user, and temperature_client.py from your unprivileged user.

References

gRPC: https://grpc.io

gRPC QuickStart guide: https://grpc.io/docs/languages/ruby/quickstart/

protobuf: https://developers.google.com/protocol-buffers/

Getting CPU temperature using Python?

Py-cputemp seems to do the job.

Get CPU temperature in python on windows

According to the MSDN page on WMI Error Constants, the error you have received is:

WBEM_E_NOT_SUPPORTED

2147749900 (0x8004100C)

Feature or operation is not supported.

Presumably, then, your CPU does not provide temperature information through WMI. If your CPU doesn't expose this information, you're probably out of luck, at least as far as a straightforward solution in Python goes.

I assume you've tried the other option given in the answer you linked, using Win32_TemperatureProbe(); if you haven't, try it.



Related Topics



Leave a reply



Submit