pywin32_bootstrap.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  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. import os
  10. import site
  11. # The directory should be installed under site-packages.
  12. dirname = os.path.dirname
  13. # This is to get the "...\Lib\site-packages" directory
  14. # out of this file name: "...\Lib\site-packages\win32\Lib\pywin32_bootstrap.py".
  15. # It needs to be searched when installed in virtual environments.
  16. level3_up_dir = dirname(dirname(dirname(__file__)))
  17. site_packages_dirs = getattr(site, "getsitepackages", lambda: [])()
  18. if level3_up_dir not in site_packages_dirs:
  19. site_packages_dirs.insert(0, level3_up_dir)
  20. for site_packages_dir in site_packages_dirs:
  21. pywin32_system32 = os.path.join(site_packages_dir, "pywin32_system32")
  22. if os.path.isdir(pywin32_system32):
  23. if hasattr(os, "add_dll_directory"):
  24. os.add_dll_directory(pywin32_system32)
  25. # This is to ensure the pywin32 path is in the beginning to find the
  26. # pywin32 DLLs first and prevent other PATH entries to shadow them
  27. elif not os.environ["PATH"].startswith(pywin32_system32):
  28. os.environ["PATH"] = os.environ["PATH"].replace(os.pathsep + pywin32_system32, "")
  29. os.environ["PATH"] = pywin32_system32 + os.pathsep + os.environ["PATH"]
  30. break