I'm using Python WMI module to collect hardware information from a Windows system. This line raises an exception:
numa = c.Win32_PerfFormattedData_PerfOS_NUMANodeMemory()
Traceback (most recent call last):
File "wmi.py", line 1209, in __getattr__
File "wmi.py", line 1220, in _cached_classes
File "<COMObject winmgmts:>", line 3, in Get
File "win32com\client\dynamic.py", line 287, in _ApplyTypes_
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemServicesEx', None, None, 0, -1073738817), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "health_check.py", line 1138, in <module>
File "health_check.py", line 1121, in main
File "health_check.py", line 228, in hardware_info
File "wmi.py", line 1211, in __getattr__
File "win32com\client\dynamic.py", line 527, in __getattr__
AttributeError: winmgmts:.Win32_PerfFormattedData_PerfOS_NUMANodeMemory
So I tried to catch it like this:
import pywintypes.com_error
try:
numa = c.Win32_PerfFormattedData_PerfOS_NUMANodeMemory()
except pywintypes.com_error:
logger.error('Failed to read NUMA status.')
But the import fails:
Traceback (most recent call last):
File "health_check.py", line 20, in <module>
ModuleNotFoundError: No module named 'pywintypes.com_error'; 'pywintypes' is not a package
[9464] Failed to execute script health_check
I tried not importing the com_error
class but that didn't work either. How can I find out the correct import to catch this type of exception? After importing wmi
the module exists here:
>>> sys.modules['pywintypes']
<module 'pywintypes' (C:\WINDOWS\SYSTEM32\pywintypes37.dll)>
>>> sys.modules['pywintypes'].com_error
<class 'pywintypes.com_error'>