pyiboot01_bootstrap.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2005-2021, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. ### Start bootstrap process
  12. # Only python built-in modules can be used.
  13. import sys
  14. import pyimod03_importers
  15. # Extend Python import machinery by adding PEP302 importers to sys.meta_path.
  16. pyimod03_importers.install()
  17. ### Bootstrap process is complete.
  18. # We can use other python modules (e.g. os)
  19. import os
  20. # Let other python modules know that the code is running in frozen mode.
  21. if not hasattr(sys, 'frozen'):
  22. sys.frozen = True
  23. # sys._MEIPASS is now set in the bootloader. Hooray.
  24. # Python 3 C-API function Py_SetPath() resets sys.prefix to empty string.
  25. # Python 2 was using PYTHONHOME for sys.prefix. Let's do the same for Python 3.
  26. sys.prefix = sys._MEIPASS
  27. sys.exec_prefix = sys.prefix
  28. # Python 3.3+ defines also sys.base_prefix. Let's set them too.
  29. sys.base_prefix = sys.prefix
  30. sys.base_exec_prefix = sys.exec_prefix
  31. # Some packages behaves differently when running inside virtual environment.
  32. # E.g. IPython tries to append path VIRTUAL_ENV to sys.path.
  33. # For the frozen app we want to prevent this behavior.
  34. VIRTENV = 'VIRTUAL_ENV'
  35. if VIRTENV in os.environ:
  36. # On some platforms (e.g. AIX) 'os.unsetenv()' is not available and then
  37. # deleting the var from os.environ does not delete it from the environment.
  38. os.environ[VIRTENV] = ''
  39. del os.environ[VIRTENV]
  40. # Ensure sys.path contains absolute paths. Otherwise import of other python
  41. # modules will fail when current working directory is changed by frozen
  42. # application.
  43. python_path = []
  44. for pth in sys.path:
  45. python_path.append(os.path.abspath(pth))
  46. sys.path = python_path
  47. # Implement workaround for prints in non-console mode. In non-console mode
  48. # (with "pythonw"), print randomly fails with "[errno 9] Bad file descriptor"
  49. # when the printed text is flushed (eg: buffer full); this is because the
  50. # sys.stdout object is bound to an invalid file descriptor.
  51. # Python 3000 has a fix for it (http://bugs.python.org/issue1415), but we
  52. # feel that a workaround in PyInstaller is a good thing since most people
  53. # found this problem for the first time with PyInstaller as they don't
  54. # usually run their code with "pythonw" (and it's hard to debug anyway).
  55. class NullWriter:
  56. softspace = 0
  57. encoding = 'UTF-8'
  58. def write(*args):
  59. pass
  60. def flush(*args):
  61. pass
  62. # Some packages are checking if stdout/stderr is available.
  63. # e.g. youtube-dl for details see #1883
  64. def isatty(self):
  65. return False
  66. # sys.stdout/err is None in GUI mode on Windows.
  67. if sys.stdout is None:
  68. sys.stdout = NullWriter()
  69. if sys.stderr is None:
  70. sys.stderr = NullWriter()
  71. # At least on Windows, Python seems to hook up the codecs on this
  72. # import, so it's not enough to just package up all the encodings.
  73. #
  74. # It was also reported that without 'encodings' module the frozen executable
  75. # will fail to load in some configurations:
  76. #
  77. # http://www.pyinstaller.org/ticket/651
  78. #
  79. # Import 'encodings' module in a run-time hook is not enough since some
  80. # run-time hooks require this module and the order of running code from
  81. # from run-time hooks is not defined.
  82. try:
  83. import encodings
  84. except ImportError:
  85. pass
  86. # In the Python interpreter 'warnings' module is imported when 'sys.warnoptions'
  87. # is not empty. Mimic this behavior in PyInstaller.
  88. if sys.warnoptions:
  89. import warnings
  90. # Install the hooks for ctypes
  91. import pyimod04_ctypes # noqa: E402
  92. pyimod04_ctypes.install()
  93. # Make .eggs and zipfiles available at runtime
  94. d = "eggs"
  95. d = os.path.join(sys._MEIPASS, d)
  96. # Test if the 'eggs' directory exists. This allows to
  97. # opportunistically including this script into the packaged exe, even
  98. # if no eggs as found when packaging the program. (Which may be a
  99. # use-case, see issue #653.
  100. if os.path.isdir(d):
  101. for fn in os.listdir(d):
  102. sys.path.append(os.path.join(d, fn))