我尝试使用
psutil
库使用 sensors_temperatures()
,但它给了我一个 AttributeError。这是我的代码:
import psutil
print(psutil.sensors_temperatures())
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-28-fbefe6006680> in <module>
----> 1 print(psutil.sensors_temperatures())
AttributeError: module 'psutil' has no attribute 'sensors_temperatures'
而且,当我用
sensors_temperatures()
检查时,
psutil
这个功能似乎不再存在于
help(psutil)
中
先生,您可以使用简单的代码使用Python来查找CPU温度。您不必仅使用“psutil”来查找温度。 您可以使用此处描述的 WMI 模块 + Open Hardware Monitor + 其 WMI 接口。
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.
def sensors_info():
try:
if hasattr(psutil, 'sensors_temperatures'):
print("Temperature Sensors:", psutil.sensors_temperatures())
else:
print("Temperature Sensors: Not supported on this system")
if hasattr(psutil, 'sensors_fans'):
print("Fan Sensors:", psutil.sensors_fans())
else:
print("Fan Sensors: Not supported on this system")
if hasattr(psutil, 'sensors_battery'):
print("Battery Status:", psutil.sensors_battery())
else:
print("Battery Status: Not supported on this system")
except Exception as e:
print(f"An error occurred while retrieving sensors info: {e}")
您的系统可能不支持其中某些功能,这就是出现错误的原因。