Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions win32/Lib/pywin32_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

try:
import pywin32_system32
except ImportError: # Python ≥3.6: replace ImportError with ModuleNotFoundError
except ImportError:
pass
else:
import os
Expand All @@ -17,5 +17,19 @@
# https://docs.python.org/3/reference/import.html#path-attributes-on-modules
for path in pywin32_system32.__path__:
if os.path.isdir(path):
os.add_dll_directory(path)
# First try the preferred method
if hasattr(os, "add_dll_directory"):
os.add_dll_directory(path)
# If `add_dll_directory` is missing, which can happen in Pylance early initialization,
# try to modify PATH if it exists (just create it if it doesn't)
elif "PATH" not in os.environ:
os.environ["PATH"] = path
else:
# This is to ensure the pywin32 path is in the beginning to find the
# pywin32 DLLs first and prevent other PATH entries to shadow them
prepend_to_path = path + os.pathsep
if not os.environ["PATH"].startswith(prepend_to_path):
os.environ["PATH"] = prepend_to_path + os.environ["PATH"].replace(
os.pathsep + path, ""
)
break