From 03cd0546567978b513408a82f3cb726d16a9ca28 Mon Sep 17 00:00:00 2001 From: Devel-Dan Date: Thu, 1 May 2025 21:41:20 -0700 Subject: [PATCH 1/2] Fix pywin32_bootstrap.py to handle early initialization environments --- win32/Lib/pywin32_bootstrap.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/win32/Lib/pywin32_bootstrap.py b/win32/Lib/pywin32_bootstrap.py index 0161f27aff..d15e128f52 100644 --- a/win32/Lib/pywin32_bootstrap.py +++ b/win32/Lib/pywin32_bootstrap.py @@ -8,7 +8,7 @@ try: import pywin32_system32 -except ImportError: # Python ≥3.6: replace ImportError with ModuleNotFoundError +except ImportError: pass else: import os @@ -17,5 +17,18 @@ # 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) + try: + # First try the preferred method + os.add_dll_directory(path) + except Exception: + # If anything fails, try to modify PATH if it exists + try: + if "PATH" in os.environ: + os.environ["PATH"] = path + os.pathsep + os.environ["PATH"] + else: + # If PATH doesn't exist, just create it + os.environ["PATH"] = path + except Exception: + # Last resort - if nothing works, just pass silently + pass break From 3c35b484cde1ba9201f2159bdafd8e88a42ce89c Mon Sep 17 00:00:00 2001 From: day kay <12646261+Devel-Dan@users.noreply.github.com> Date: Sun, 4 May 2025 15:16:44 -0700 Subject: [PATCH 2/2] Update win32/Lib/pywin32_bootstrap.py Co-authored-by: Avasam --- win32/Lib/pywin32_bootstrap.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/win32/Lib/pywin32_bootstrap.py b/win32/Lib/pywin32_bootstrap.py index d15e128f52..0531db66ad 100644 --- a/win32/Lib/pywin32_bootstrap.py +++ b/win32/Lib/pywin32_bootstrap.py @@ -17,18 +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): - try: - # First try the preferred method + # First try the preferred method + if hasattr(os, "add_dll_directory"): os.add_dll_directory(path) - except Exception: - # If anything fails, try to modify PATH if it exists - try: - if "PATH" in os.environ: - os.environ["PATH"] = path + os.pathsep + os.environ["PATH"] - else: - # If PATH doesn't exist, just create it - os.environ["PATH"] = path - except Exception: - # Last resort - if nothing works, just pass silently - pass + # 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