Skip to content

Commit 33f3e9c

Browse files
committed
Fix pywin32_bootstrap.py to handle early initialization environments
1 parent d764900 commit 33f3e9c

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

win32/Lib/pywin32_bootstrap.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
try:
1010
import pywin32_system32
11-
except ImportError: # Python ≥3.6: replace ImportError with ModuleNotFoundError
11+
except ImportError:
1212
pass
1313
else:
1414
import os
@@ -17,5 +17,18 @@
1717
# https://docs.python.org/3/reference/import.html#path-attributes-on-modules
1818
for path in pywin32_system32.__path__:
1919
if os.path.isdir(path):
20-
os.add_dll_directory(path)
20+
try:
21+
# First try the preferred method
22+
os.add_dll_directory(path)
23+
except Exception:
24+
# If anything fails, try to modify PATH if it exists
25+
try:
26+
if 'PATH' in os.environ:
27+
os.environ['PATH'] = path + os.pathsep + os.environ['PATH']
28+
else:
29+
# If PATH doesn't exist, just create it
30+
os.environ['PATH'] = path
31+
except Exception:
32+
# Last resort - if nothing works, just pass silently
33+
pass
2134
break

0 commit comments

Comments
 (0)