Windows Devices Enumeration (WinRT)
This package provides the Python projection for the Windows.Devices.Enumeration namespace of the Windows Runtime (WinRT) APIs. It allows Python developers to discover and manage hardware devices available on a Windows system. Part of the larger PyWinRT project, it is currently at version 3.2.1 and follows the PyWinRT release cadence, often tied to Windows SDK updates.
Common errors
-
ModuleNotFoundError: No module named 'winrt.windows.devices.enumeration'
cause The `winrt-windows-devices-enumeration` package is not installed, or there's a typo in the import path.fixEnsure the package is installed via `pip install winrt-windows-devices-enumeration`. Double-check the import statement for typos. -
TypeError: 'winrt.windows.foundation.IAsyncOperation_1[winrt.windows.devices.enumeration.DeviceInformationCollection]' object is not iterable
cause An asynchronous WinRT operation, such as `DeviceInformation.FindAllAsync()`, was called but not `awaited`. The method returns an awaitable object, not the final result.fixPrepend `await` to the asynchronous call: `devices = await DeviceInformation.FindAllAsync()`. Ensure the surrounding code runs in an `async` function and is executed via `asyncio.run()`. -
AttributeError: 'Object' object has no attribute 'name'
cause This error often occurs after upgrading to `v3.0.0` if old patterns for type casting or accessing properties are still used. `Object.as_()` was introduced for explicit type casting.fixEnsure you are using `obj.as_(TargetType)` for explicit type casting where necessary, especially when dealing with generic `winrt.system.Object` types or after deserialization. Verify the object's actual type at runtime.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, including a new `winrt.runtime` module, changes to `box`/`unbox` functions (moved to `winrt.system`), and the introduction of `winrt.system.Object.as_()` for type casting. Older code will likely fail.
- gotcha WinRT asynchronous operations (methods ending in `Async`, e.g., `FindAllAsync()`) return awaitable objects. Forgetting to `await` them will result in an `Awaitable` object instead of the final result, leading to type errors if you try to use it directly.
- gotcha Version 2.0.0 was never published to PyPI due to compilation issues. If you target `2.x`, attempting to install `2.0.0` will fail or result in an outdated package.
- breaking In `v2.1.0`, the handling of static events was changed, moving them to the metaclass of the respective WinRT class. Code accessing static events might need adjustment.
Install
-
pip install winrt-windows-devices-enumeration
Imports
- DeviceInformation
from winrt.windows.devices.enumeration import DeviceInformation
- DeviceWatcher
from winrt.windows.devices.enumeration import DeviceWatcher
Quickstart
import asyncio
from winrt.windows.devices.enumeration import DeviceInformation
async def list_devices():
print("Searching for devices...")
# Find all devices asynchronously
devices = await DeviceInformation.FindAllAsync()
if devices.size == 0:
print("No devices found.")
return
print(f"Found {devices.size} devices:")
for i in range(devices.size):
device = devices.get_at(i)
print(f" [{i+1}] Name: {device.name}, Id: {device.id}")
if __name__ == "__main__":
# Ensure to run async code using asyncio
asyncio.run(list_devices())