pywin32_bootstrap.py 1.3 KB

12345678910111213141516171819202122232425262728
  1. # Imported by pywin32.pth to bootstrap the pywin32 environment in "portable"
  2. # environments or any other case where the post-install script isn't run.
  3. #
  4. # In short, there's a directory installed by pywin32 named 'pywin32_system32'
  5. # with some important DLLs which need to be found by Python when some pywin32
  6. # modules are imported.
  7. # If Python has `os.add_dll_directory()`, we need to call it with this path.
  8. # Otherwise, we add this path to PATH.
  9. try:
  10. import pywin32_system32
  11. except ImportError: # Python ≥3.6: replace ImportError with ModuleNotFoundError
  12. pass
  13. else:
  14. import os
  15. # We're guaranteed only that __path__: Iterable[str]
  16. # https://docs.python.org/3/reference/import.html#__path__
  17. for path in pywin32_system32.__path__:
  18. if os.path.isdir(path):
  19. if hasattr(os, "add_dll_directory"):
  20. os.add_dll_directory(path)
  21. # This is to ensure the pywin32 path is in the beginning to find the
  22. # pywin32 DLLs first and prevent other PATH entries to shadow them
  23. elif not os.environ["PATH"].startswith(path):
  24. os.environ["PATH"] = os.environ["PATH"].replace(os.pathsep + path, "")
  25. os.environ["PATH"] = path + os.pathsep + os.environ["PATH"]
  26. break