From 71afa71e11e6631be611ca5cb57cda526b5e91ab Mon Sep 17 00:00:00 2001 From: Mark Hammond Date: Tue, 5 Nov 2019 14:59:33 +1100 Subject: [PATCH] Change how the directory with the pywin32 DLLs is treated at startup. This directory is now added to the start of PATH for version 3.7 and earlier, and passed to os.add_dll_directory() on 3.8 and later. This will hopefully work around problems loading pywintypes.dll in various situations. Fixes #1432, fixes #1431 --- CHANGES.txt | 5 +++++ pywin32.pth | 8 +++----- win32/Lib/pywin32_bootstrap.py | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 win32/Lib/pywin32_bootstrap.py diff --git a/CHANGES.txt b/CHANGES.txt index e306b1e8aa..312abe2744 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -6,6 +6,11 @@ However contributors are encouraged to add their own entries for their work. Since build 225: ---------------- +* The directory with the pywin32 system DLLs is now added to the start of PATH + for version 3.7 and earlier, and passed to os.add_dll_directory() on 3.8 and + later. This will hopefully work around problems loading pywintypes.dll in + various situations. + * Conversions to and from COM VT_DATE types should no longer lose milliseconds. Since build 224: diff --git a/pywin32.pth b/pywin32.pth index b885ba018a..b57c49647b 100644 --- a/pywin32.pth +++ b/pywin32.pth @@ -2,8 +2,6 @@ win32 win32\lib Pythonwin -# Entries needed for a "portable" installations, where the post_install script -# isn't run, which would normally copy the pywin32 core DLL files to either -# the top of the python directory. -# We just stick the source of these DLLs directly on the PATH. -import os;pywin32_system32=os.path.join(sitedir,"pywin32_system32");os.environ["PATH"]+=('' if pywin32_system32 in os.environ["PATH"] else (';'+pywin32_system32)) +# And some hackery to deal with environments where the post_install script +# isn't run. +import pywin32_bootstrap diff --git a/win32/Lib/pywin32_bootstrap.py b/win32/Lib/pywin32_bootstrap.py new file mode 100644 index 0000000000..6f4fdd18a9 --- /dev/null +++ b/win32/Lib/pywin32_bootstrap.py @@ -0,0 +1,21 @@ +# Imported by pywin32.pth to bootstrap the pywin32 environment in "portable" +# environments or any other case where the post-install script isn't run. +# +# In short, there's a directory installed by pywin32 named 'pywin32_system32' +# with some important DLLs which need to be found by Python when some pywin32 +# modules are imported. +# If Python has `os.add_dll_directory()`, we need to call it with this path. +# Otherwise, we add this path to PATH. +import os +import site +import sys + +# The directory should be installed under site-packages. +for maybe in site.getsitepackages(): + pywin32_system32=os.path.join(maybe,"pywin32_system32") + if os.path.isdir(pywin32_system32): + if hasattr(os, "add_dll_directory"): + os.add_dll_directory(pywin32_system32) + else: + os.environ["PATH"] = pywin32_system32 + ";" + os.environ["PATH"] +