Skip to content

Commit a017509

Browse files
Add pybind extension to the DLL search path on Windows (#14469)
Co-authored-by: Gregory Comer <[email protected]>
1 parent cae59f8 commit a017509

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)