From b3933d842a9dcecbd72143701d70612b321c520e Mon Sep 17 00:00:00 2001 From: Gregory Comer Date: Fri, 19 Sep 2025 12:20:17 -0700 Subject: [PATCH] Add pybind extension to the dll search path on Windows --- extension/pybindings/portable_lib.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/extension/pybindings/portable_lib.py b/extension/pybindings/portable_lib.py index da65983cf02..0982d55b474 100644 --- a/extension/pybindings/portable_lib.py +++ b/extension/pybindings/portable_lib.py @@ -13,6 +13,9 @@ This API is experimental and subject to change without notice. """ +import logging +import os +import sys import warnings as _warnings import executorch.exir._warnings as _exir_warnings @@ -28,6 +31,21 @@ # dependencies. import torch as _torch +logger = logging.getLogger(__name__) + +# Update the DLL search path on Windows. This is the recommended way to handle native +# extensions. +if sys.platform == "win32": + try: + # The extension DLL should be in the same directory as this file. + pybindings_dir = os.path.dirname(os.path.abspath(__file__)) + os.add_dll_directory(pybindings_dir) + except Exception as e: + logger.error( + "Failed to add the pybinding extension DLL to the search path. The extension may not work.", + e, + ) + # Let users import everything from the C++ _portable_lib extension as if this # python file defined them. Although we could import these dynamically, it # wouldn't preserve the static type annotations.