WinRT Windows Foundation API
winrt-windows-foundation is a Python package that provides a projection of the core Windows Runtime (WinRT) Foundation APIs, enabling Python developers to interact with fundamental Windows features and UWP application components. As of version 3.2.1, it offers enhanced asyncio integration, support for the Python buffer protocol, and streamlined API access. Releases are frequent, often coinciding with updates to the Windows SDK and Windows App SDK.
Common errors
-
ModuleNotFoundError: No module named 'winrt.runtime'
cause The `winrt.runtime` module, previously provided by the separate `winrt-runtime` PyPI package (or a different internal structure), has been integrated into the main `winrt-windows-foundation` and other `winrt-*` packages since v3.0.0.fixEnsure `winrt-windows-foundation` is updated to v3.0.0 or later. Remove any explicit `pip uninstall winrt-runtime` if it was installed separately. The `winrt.runtime` module is now part of the standard `winrt` package hierarchy. -
AttributeError: 'Object' object has no attribute '__eq__'
cause Attempting to use equality operators (`==`, `!=`) on `winrt.system.Object` instances before `v3.0.0`, which did not support these operations.fixUpgrade `winrt-windows-foundation` to `v3.0.0` or later, which introduced support for `==`, `!=`, and `hash()` for `winrt.system.Object`. -
TypeError: unhashable type: 'Object'
cause Attempting to use `winrt.system.Object` instances as dictionary keys or in sets before `v3.0.0`, which did not implement `__hash__`.fixUpgrade `winrt-windows-foundation` to `v3.0.0` or later, which introduced support for `hash()` for `winrt.system.Object`. -
AttributeError: module 'winrt.windows.foundation.interop' has no attribute 'box'
cause The `box` and `unbox` functions were moved and refactored into type-specific functions under `winrt.system` in `v3.0.0`.fixUpdate import statements and function calls to use type-specific functions directly from `winrt.system`, e.g., `from winrt.system import box_int32` instead of `from winrt.windows.foundation.interop import box`.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, including major changes to module structure, `winrt.system.Object` behavior (e.g., for equality), and the relocation/refactoring of `box`/`unbox` functions. Consult the migration guide for details.
- deprecated The separate `winrt-runtime` PyPI package is no longer needed nor compatible with `winrt-windows-foundation` versions 3.0.0 and above. Its functionality is now integrated directly into `winrt-*` packages.
- gotcha Before `v3.1.0`, `IIterator.__iter__()` contained critical bugs that could lead to crashes or incorrect object returns, especially when iterating over WinRT collections.
- breaking The `box` and `unbox` helper functions, previously found in `winrt.windows.foundation.interop`, were moved to `winrt.system` and split into type-specific functions (e.g., `box_int32`, `unbox_string`) in `v3.0.0`.
- gotcha The `v2.0.0` release was never published to PyPI due to compilation issues. If you intend to use a `v2` release, `v2.0.1` was the first stable and published version.
Install
-
pip install winrt-windows-foundation
Imports
- Uri
from winrt.windows.foundation import Uri
- IAsyncOperation
from winrt.windows.foundation import IAsyncOperation
- PropertyValue
from winrt.windows.foundation import PropertyValue
- box_int32
from winrt.windows.foundation.interop import box
from winrt.system import box_int32
Quickstart
from winrt.windows.foundation import Uri
def main():
# Create a WinRT Uri object
url = Uri("https://www.microsoft.com/python")
print(f"Original URI: {url.AbsoluteUri}")
print(f"Scheme Name: {url.SchemeName}")
print(f"Domain: {url.Domain}")
print(f"Path: {url.Path}")
# Demonstrate a simple comparison
another_url = Uri("https://www.microsoft.com/python")
if url == another_url:
print("URIs are equal.")
if __name__ == "__main__":
main()