Windows下Python获取AMD CPU温度失败的解决方案问询
在Windows 11 24H2 + Python 3.10.6环境下获取AMD CPU温度失败的问题及需求
环境与问题概述
- 系统:Windows 11 24H2
- Python版本:3.10.6
- 测试CPU:Ryzen 9 3950X、Ryzen 7 2700X
- 问题:尝试多种Python方法获取CPU温度均失效,包括WinTmp库、调用LibreHardwareMonitor/OpenHardwareMonitor DLL、WMI/MSAcpi方法,且Stack Overflow相关问题的代码也全部无效。
1. WinTmp库测试
使用以下代码调用:
import WinTmp print(WinTmp.CPU_Temp())
返回结果:
>>> 0.0
结果明显不符合实际CPU温度。
2. LibreHardwareMonitor/OpenHardwareMonitor DLL + WMI/MSAcpi方法测试
完整测试代码:
import clr from pathlib import Path import os try: import pynvml except ImportError: pynvml = None try: import win32com.client except ImportError: win32com = None def initialize_hardware_monitor(): try: dll_path = str(Path(__file__).parent / "LibreHardwareMonitorLib.dll") if not os.path.exists(dll_path): dll_path = str(Path(__file__).parent / "OpenHardwareMonitorLib.dll") if not os.path.exists(dll_path): print("Error: LibreHardwareMonitorLib.dll or OpenHardwareMonitorLib.dll not found.") return None clr.AddReference(dll_path) from LibreHardwareMonitor import Hardware handle = Hardware.Computer() handle.IsCpuEnabled = True handle.IsGpuEnabled = True handle.Open() print("Hardware Monitor initialized successfully") return handle except Exception as e: print(f"Error initializing Hardware Monitor: {e}") return None def get_wmi_msacpi_temperature(sensor_type="cpu"): if not win32com: print(f"pywin32 not installed for {sensor_type.upper()}") return None try: wmi_obj = win32com.client.GetObject("winmgmts:\\.\root\wmi") sensors = wmi_obj.ExecQuery("SELECT * FROM MSAcpi_ThermalZoneTemperature") for sensor in sensors: temp_kelvin = sensor.CurrentTemperature / 10.0 temp_celsius = temp_kelvin - 273.15 if sensor_type in sensor.InstanceName.lower() and 0 < temp_celsius < 100: return round(temp_celsius, 1) print(f"No {sensor_type.upper()} temperature sensor in MSAcpi_ThermalZoneTemperature") return None except Exception as e: print(f"Error getting {sensor_type.upper()} temperature via MSAcpi: {e}") return None def get_cpu_temperature(): try: handle = initialize_hardware_monitor() if handle: for hardware in handle.Hardware: hardware.Update() if hardware.HardwareType == 2: for sensor in hardware.Sensors: if sensor.SensorType == 2 and "cpu" in sensor.Name.lower(): temp = round(sensor.Value, 1) if 0 < temp < 100: return temp print("No CPU temperature sensor found in Hardware Monitor") except Exception as e: print(f"Error getting CPU temperature via Hardware Monitor: {e}") # WMI temp = get_wmi_msacpi_temperature("cpu") if temp: return temp print("All CPU temperature methods failed.") return None print(get_cpu_temperature())
运行输出:
Hardware Monitor initialized successfully No CPU temperature sensor found in Hardware Monitor Error getting CPU temperature via MSAcpi: (-2147217405, 'OLE error 0x80041003', None, None) All CPU temperature methods failed. None
问题点:
- DLL初始化成功,但遍历后找不到CPU温度传感器
- WMI/MSAcpi调用直接报错,部分测试场景返回错误值(如0℃、29℃)
核心需求
- 无需运行OpenHardwareMonitor(OHM)主程序,仅通过其DLL实现CPU温度获取
- 希望详细了解Python调用C#/C++ DLL的完整流程
- 若上述需求无法实现,请明确告知以便转向其他技术方案
内容的提问来源于stack exchange,提问作者Tim Ryzikov
相关产品推荐
相关产品推荐

