pyi_rth_pyqt6.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2021-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. # The path to Qt's components may not default to the wheel layout for self-compiled PyQt6 installations. Mandate the
  12. # wheel layout. See ``utils/hooks/qt.py`` for more details.
  13. def _pyi_rthook():
  14. import os
  15. import sys
  16. from _pyi_rth_utils import is_macos_app_bundle, prepend_path_to_environment_variable
  17. from _pyi_rth_utils import qt as qt_rth_utils
  18. # Ensure this is the only Qt bindings package in the application.
  19. qt_rth_utils.ensure_single_qt_bindings_package("PyQt6")
  20. # Try PyQt6 6.0.3-style path first...
  21. pyqt_path = os.path.join(sys._MEIPASS, 'PyQt6', 'Qt6')
  22. if not os.path.isdir(pyqt_path):
  23. # ... and fall back to the older version.
  24. pyqt_path = os.path.join(sys._MEIPASS, 'PyQt6', 'Qt')
  25. os.environ['QT_PLUGIN_PATH'] = os.path.join(pyqt_path, 'plugins')
  26. if is_macos_app_bundle:
  27. # Special handling for macOS .app bundles. To satisfy codesign requirements, we are forced to split `qml`
  28. # directory into two parts; one that keeps only binaries (rooted in `Contents/Frameworks`) and one that keeps
  29. # only data files (rooted in `Contents/Resources), with files from one directory tree being symlinked to the
  30. # other to maintain illusion of a single mixed-content directory. As Qt seems to compute the identifier of its
  31. # QML components based on location of the `qmldir` file w.r.t. the registered QML import paths, we need to
  32. # register both paths, because the `qmldir` file for a component could be reached via either directory tree.
  33. pyqt_path_res = os.path.normpath(
  34. os.path.join(sys._MEIPASS, '..', 'Resources', os.path.relpath(pyqt_path, sys._MEIPASS))
  35. )
  36. os.environ['QML2_IMPORT_PATH'] = os.pathsep.join([
  37. os.path.join(pyqt_path_res, 'qml'),
  38. os.path.join(pyqt_path, 'qml'),
  39. ])
  40. else:
  41. os.environ['QML2_IMPORT_PATH'] = os.path.join(pyqt_path, 'qml')
  42. # Add `sys._MEIPASS` to `PATH` in order to ensure that `QtNetwork` can discover OpenSSL DLLs that might have been
  43. # collected there (i.e., when they were not shipped with the package, and were collected from an external location).
  44. if sys.platform.startswith('win'):
  45. prepend_path_to_environment_variable(sys._MEIPASS, 'PATH')
  46. # For macOS POSIX builds, we need to add `sys._MEIPASS` to `DYLD_LIBRARY_PATH` so that QtNetwork can discover
  47. # OpenSSL dynamic libraries for its `openssl` TLS backend. This also prevents fallback to external locations, such
  48. # as Homebrew. For .app bundles, this is unnecessary because `QtNetwork` explicitly searches `Contents/Frameworks`.
  49. if sys.platform == 'darwin' and not is_macos_app_bundle:
  50. prepend_path_to_environment_variable(sys._MEIPASS, 'DYLD_LIBRARY_PATH')
  51. # Qt bindings package installed via PyPI wheels typically ensures that its bundled Qt is relocatable, by creating
  52. # embedded `qt.conf` file during its initialization. This run-time generated qt.conf dynamically sets the Qt prefix
  53. # path to the package's Qt directory. For bindings packages that do not create embedded `qt.conf` during their
  54. # initialization (for example, conda-installed packages), try to perform this step ourselves.
  55. qt_rth_utils.create_embedded_qt_conf("PyQt6", pyqt_path)
  56. _pyi_rthook()
  57. del _pyi_rthook