pyi_rth_setuptools.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2022-2023, PyInstaller Development Team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: Apache-2.0
  10. #-----------------------------------------------------------------------------
  11. # This runtime hook performs the equivalent of the distutils-precedence.pth from the setuptools package;
  12. # it registers a special meta finder that diverts import of distutils to setuptools._distutils, if available.
  13. def _pyi_rthook():
  14. def _install_setuptools_distutils_hack():
  15. import os
  16. import setuptools
  17. # We need to query setuptools version at runtime, because the default value for SETUPTOOLS_USE_DISTUTILS
  18. # has changed at version 60.0 from "stdlib" to "local", and we want to mimic that behavior.
  19. setuptools_major = int(setuptools.__version__.split('.')[0])
  20. default_value = "stdlib" if setuptools_major < 60 else "local"
  21. if os.environ.get("SETUPTOOLS_USE_DISTUTILS", default_value) == "local":
  22. import _distutils_hack
  23. _distutils_hack.add_shim()
  24. try:
  25. _install_setuptools_distutils_hack()
  26. except Exception:
  27. pass
  28. _pyi_rthook()
  29. del _pyi_rthook