WinRT Windows Devices Radios
winrt-windows-devices-radios is a Python projection of a subset of Windows Runtime (WinRT) APIs, specifically those related to managing wireless radios like Wi-Fi and Bluetooth. It allows Python applications to interact with system hardware for radio control on Windows. Part of the broader `pywinrt` project, it is currently at version 3.2.1 and receives regular updates corresponding to new Windows SDK releases and `pywinrt` core improvements.
Common errors
-
ModuleNotFoundError: No module named 'winrt.windows.devices.radios'
cause The `winrt-windows-devices-radios` package is not installed or the Python environment is incorrect.fixRun `pip install winrt-windows-devices-radios` to install the package in your active Python environment. -
RuntimeError: The current thread has no asyncio event loop.
cause An `await` call was made outside of an `async` function, or an `async` function was called without `asyncio.run()` or a running event loop.fixEnsure all async WinRT operations are `await`ed inside `async` functions, and that your main function uses `asyncio.run(your_async_main_function())`. -
ImportError: DLL load failed while importing _winrt_windows_devices_radios: The specified module could not be found.
cause This error typically occurs if a necessary Windows DLL (e.g., Visual C++ Redistributable, or a core Windows component) is missing, corrupted, or if the Python environment's architecture (32-bit vs. 64-bit) does not match the Windows installation, or if attempting to run on non-Windows.fixVerify you are on a compatible Windows OS. Ensure your Python installation matches your system architecture (usually 64-bit). Try reinstalling the `winrt-windows-devices-radios` package. Ensure relevant Windows system components are up to date.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, including a new `winrt.runtime` module, changes to boxing/unboxing functions, and how `winrt.system.Object` methods like `as_()` are used. Refer to the migration guide for details.
- gotcha This library is a projection of Windows Runtime (WinRT) APIs and is therefore strictly Windows-only. Attempting to install or run it on non-Windows operating systems will result in `ImportError` or `ModuleNotFoundError`.
- gotcha Many WinRT methods are asynchronous, indicated by names ending in `_async` (e.g., `get_radios_async`). These methods return `IAsyncOperation` or `IAsyncAction` objects and must be `await`ed within an `asyncio` event loop.
Install
-
pip install winrt-windows-devices-radios -
pip install pywinrt
Imports
- Radio
import winrt.windows.devices.radios.Radio
from winrt.windows.devices.radios import Radio
- RadioState
from winrt.windows.devices.radios import RadioState
Quickstart
import asyncio
from winrt.windows.devices.radios import Radio, RadioState
async def list_radios():
try:
radios = await Radio.get_radios_async()
if not radios:
print("No radios found.")
return
print(f"Found {len(radios)} radios:")
for radio in radios:
print(f" Name: {radio.name}")
print(f" Kind: {radio.kind}")
print(f" State: {radio.state}")
# Example of setting state (requires appropriate user permissions)
# if radio.state == RadioState.ON:
# print(f" Attempting to turn OFF {radio.name}...")
# # await radio.set_state_async(RadioState.OFF)
# # print(f" Turned OFF {radio.name}")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure you are running on Windows with necessary permissions.")
if __name__ == "__main__":
# os.environ.get('WINRT_DEBUG', 'false') # Example for auth check if needed
asyncio.run(list_radios())