Skip to content

Commit b1aed62

Browse files
authored
Add pybind extension to the DLL search path on Windows (#14446)
### Summary When installing ExecuTorch from a wheel on Windows, the native pybinding extension DLL fails to load (#14443). This is because it's not on the search path. From a quick Google search, it seems like the recommended way to handle this is to call `os.add_dll_directory` prior to loading the extension. I tested this locally by installing from wheel in a clean env and then patching the installed copy in site-packages. With this change, I was able to import executorch.extension.pybindings.portable_lib. I also ran the XNNPACK add op tests as a sanity check, which use pybindings.
1 parent ecb8f83 commit b1aed62

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

extension/pybindings/portable_lib.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
This API is experimental and subject to change without notice.
1414
"""
1515

16+
import logging
17+
import os
18+
import sys
1619
import warnings as _warnings
1720

1821
import executorch.exir._warnings as _exir_warnings
@@ -28,6 +31,21 @@
2831
# dependencies.
2932
import torch as _torch
3033

34+
logger = logging.getLogger(__name__)
35+
36+
# Update the DLL search path on Windows. This is the recommended way to handle native
37+
# extensions.
38+
if sys.platform == "win32":
39+
try:
40+
# The extension DLL should be in the same directory as this file.
41+
pybindings_dir = os.path.dirname(os.path.abspath(__file__))
42+
os.add_dll_directory(pybindings_dir)
43+
except Exception as e:
44+
logger.error(
45+
"Failed to add the pybinding extension DLL to the search path. The extension may not work.",
46+
e,
47+
)
48+
3149
# Let users import everything from the C++ _portable_lib extension as if this
3250
# python file defined them. Although we could import these dynamically, it
3351
# wouldn't preserve the static type annotations.

0 commit comments

Comments
 (0)