pyiboot01_bootstrap.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 # noqa: E402
  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. Python 2 was using PYTHONHOME for sys.prefix.
  25. # 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 behave differently when running inside virtual environment. E.g., IPython tries to append path
  32. # VIRTUAL_ENV to sys.path. For the frozen app we want to prevent this behavior.
  33. VIRTENV = 'VIRTUAL_ENV'
  34. if VIRTENV in os.environ:
  35. # On some platforms (e.g., AIX) 'os.unsetenv()' is unavailable and deleting the var from os.environ does not
  36. # delete it from the environment.
  37. os.environ[VIRTENV] = ''
  38. del os.environ[VIRTENV]
  39. # Ensure sys.path contains absolute paths. Otherwise, import of other python modules will fail when current working
  40. # directory is changed by the frozen application.
  41. python_path = []
  42. for pth in sys.path:
  43. python_path.append(os.path.abspath(pth))
  44. sys.path = python_path
  45. # Implement workaround for prints in non-console mode. In non-console mode (with "pythonw"), print randomly fails with
  46. # "[errno 9] Bad file descriptor" when the printed text is flushed (e.g., buffer full); this is because the sys.stdout
  47. # object is bound to an invalid file descriptor.
  48. # Python 3000 has a fix for it (http://bugs.python.org/issue1415), but we feel that a workaround in PyInstaller is a
  49. # good thing, because most people first encounter this problem with PyInstaller as they do not usually run their code
  50. # with "pythonw" (and it is difficult to debug, anyway).
  51. class NullWriter:
  52. softspace = 0
  53. encoding = 'UTF-8'
  54. def write(*args):
  55. pass
  56. def flush(*args):
  57. pass
  58. # Some packages are checking if stdout/stderr is available (e.g., youtube-dl). For details, see #1883.
  59. def isatty(self):
  60. return False
  61. # sys.stdout/err is None in GUI mode on Windows.
  62. if sys.stdout is None:
  63. sys.stdout = NullWriter()
  64. if sys.stderr is None:
  65. sys.stderr = NullWriter()
  66. # At least on Windows, Python seems to hook up the codecs on this import, so it is not enough to just package up all
  67. # the encodings.
  68. #
  69. # It was also reported that without 'encodings' module, the frozen executable fails to load in some configurations:
  70. # http://www.pyinstaller.org/ticket/651
  71. #
  72. # Importing 'encodings' module in a run-time hook is not enough, since some run-time hooks require this module, and the
  73. # order of running the code from the run-time hooks is not defined.
  74. try:
  75. import encodings # noqa: F401
  76. except ImportError:
  77. pass
  78. # In the Python interpreter 'warnings' module is imported when 'sys.warnoptions' is not empty. Mimic this behavior.
  79. if sys.warnoptions:
  80. import warnings # noqa: F401
  81. # Install the hooks for ctypes
  82. import pyimod04_ctypes # noqa: E402
  83. pyimod04_ctypes.install()
  84. # Make .eggs and zipfiles available at runtime
  85. d = "eggs"
  86. d = os.path.join(sys._MEIPASS, d)
  87. # Test if the 'eggs' directory exists. This allows us to opportunistically include this script into the packaged exe,
  88. # even if no eggs were found when packaging the program. (Which may be a use-case, see issue #653).
  89. if os.path.isdir(d):
  90. for fn in os.listdir(d):
  91. sys.path.append(os.path.join(d, fn))