Python Forum

Full Version: WMI error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am trying to run this thing to get the CPU temperature(I got it from youtube but I am open to other suggestions that work on WINDOWS) and I get this error.
import wmi
w_temp=wmi.WMI(namespace="root\\wmi")
print((w_temp.MSAcpi_ThermalZoneTemperature()[0].CurrentTemperature/10.0)-273.15)
Error:
Traceback (most recent call last): File "C:\Users\cosmi\PycharmProjects\pythonProject\venv\Lib\site-packages\wmi.py", line 880, in query return self._namespace.query(wql, self, fields) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cosmi\PycharmProjects\pythonProject\venv\Lib\site-packages\wmi.py", line 1072, in query return [ _wmi_object(obj, instance_of, fields) for obj in self._raw_query(wql) ] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cosmi\PycharmProjects\pythonProject\venv\Lib\site-packages\wmi.py", line 1072, in <listcomp> return [ _wmi_object(obj, instance_of, fields) for obj in self._raw_query(wql) ] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cosmi\PycharmProjects\pythonProject\venv\Lib\site-packages\win32com\client\dynamic.py", line 324, in __getitem__ return self._get_good_object_(self._enum_.__getitem__(index)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cosmi\PycharmProjects\pythonProject\venv\Lib\site-packages\win32com\client\util.py", line 41, in __getitem__ return self.__GetIndex(index) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cosmi\PycharmProjects\pythonProject\venv\Lib\site-packages\win32com\client\util.py", line 62, in __GetIndex result = self._oleobj_.Next(1) ^^^^^^^^^^^^^^^^^^^^^ pywintypes.com_error: (-2147217405, 'OLE error 0x80041003', None, None) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\cosmi\PycharmProjects\pythonProject\main.py", line 3, in <module> print((w_temp.MSAcpi_ThermalZoneTemperature()[0].CurrentTemperature/10.0)-273.15) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cosmi\PycharmProjects\pythonProject\venv\Lib\site-packages\wmi.py", line 882, in query handle_com_error() File "C:\Users\cosmi\PycharmProjects\pythonProject\venv\Lib\site-packages\wmi.py", line 258, in handle_com_error raise klass(com_error=err) wmi.x_access_denied: <x_wmi: Unexpected COM Error (-2147217405, 'OLE error 0x80041003', None, None)> Process finished with exit code 1
Many CPU producers wouldn't provide a way so wmi can acess temperature directly.
Can use Open Hardware Monitor and call it trough wmi.
import wmi

w = wmi.WMI(namespace="root\OpenHardwareMonitor")
temperature_infos = w.Sensor()
#print(temperature_infos)
for sensor in temperature_infos:
    #print(sensor)
    if sensor.SensorType == 'Temperature':
        print(sensor.Name)
        print(sensor.Value)
Output:
System 31.0 Temperature 25.0 CPU Core #3 37.0 CPU Core #4 39.0 CPU Core #5 42.0 CPU Core #6 42.0 GPU Core 34.0 Auxiliary 37.0 CPU Core #1 42.0 CPU Core #2 42.0
........
(Nov-13-2022, 11:57 PM)snippsat Wrote: [ -> ]Many CPU producers wouldn't provide a way so wmi can acess temperature directly.
Can use Open Hardware Monitor and call it trough wmi.
import wmi

w = wmi.WMI(namespace="root\OpenHardwareMonitor")
temperature_infos = w.Sensor()
#print(temperature_infos)
for sensor in temperature_infos:
    #print(sensor)
    if sensor.SensorType == 'Temperature':
        print(sensor.Name)
        print(sensor.Value)
Output:
System 31.0 Temperature 25.0 CPU Core #3 37.0 CPU Core #4 39.0 CPU Core #5 42.0 CPU Core #6 42.0 GPU Core 34.0 Auxiliary 37.0 CPU Core #1 42.0 CPU Core #2 42.0
Thank you so much this made me realize that when I tried this method I installed OpenHardware monitor in my project folder and used namespace="root\OpenHardwareMonitor".....