Skip to content

Commit

Permalink
hooks: fix pyqt5 on conda-forge (#2763)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotduarte committed Jan 4, 2025
1 parent afffd8a commit 586bef5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
3 changes: 2 additions & 1 deletion cx_Freeze/hooks/_qthooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def copy_qt_files(
) -> None:
"""Helper function to copy Qt plugins, resources, translations, etc."""
for source_path, target_path in _get_qt_files(name, variable, arg):
finder.lib_files[source_path] = target_path.as_posix()
finder.include_files(source_path, target_path)


Expand Down Expand Up @@ -191,7 +192,7 @@ def load_qt_qtcore(finder: ModuleFinder, module: Module) -> None:
name = _qt_implementation(module)
variable = "BinariesPath" if IS_WINDOWS else "LibrariesPath"
for source, target in _get_qt_files(name, variable, "*"):
finder.lib_files[source] = target
finder.lib_files.setdefault(source, target.as_posix())


def load_qt_qtdesigner(finder: ModuleFinder, module: Module) -> None:
Expand Down
39 changes: 30 additions & 9 deletions cx_Freeze/hooks/pyqt5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from textwrap import dedent
from typing import TYPE_CHECKING

from cx_Freeze._compat import IS_MACOS
from cx_Freeze._compat import IS_MACOS, IS_MINGW, IS_WINDOWS
from cx_Freeze.common import get_resource_file_path
from cx_Freeze.hooks._qthooks import copy_qt_files
from cx_Freeze.hooks._qthooks import load_qt_qtcore as load_pyqt5_qtcore
Expand Down Expand Up @@ -55,10 +55,6 @@ def load_pyqt5(finder: ModuleFinder, module: Module) -> None:
print(f"WARNING: zip_include_packages={module.name} ignored.")
module.in_file_system = 2

# Include a module that fix an issue
qt_debug = get_resource_file_path("hooks/pyqt5", "_append_to_init", ".py")
finder.include_file_as_module(qt_debug, "PyQt5._cx_freeze_append_to_init")

# Include a module that inject an optional debug code
qt_debug = get_resource_file_path("hooks/pyqt5", "debug", ".py")
finder.include_file_as_module(qt_debug, "PyQt5._cx_freeze_debug")
Expand All @@ -72,18 +68,32 @@ def load_pyqt5(finder: ModuleFinder, module: Module) -> None:
copy_qt_files(finder, "PyQt5", "LibraryExecutablesPath", "qt.conf")

# Inject code to the end of init
code_string = module.file.read_text(encoding="utf_8")
if environment == "conda":
code_string = ""
else:
code_string = module.file.read_text(encoding="utf_8")
code_string += dedent(
f"""
# cx_Freeze patch start
import os, sys
frozen_dir = sys.frozen_dir
qt_root_dir = os.path.join(frozen_dir, "lib", "PyQt5")
try:
from PyQt5 import QtCore
except ImportError:
if {IS_MINGW or IS_WINDOWS}:
bin_dir = os.path.join(qt_root_dir, "Qt5", "bin")
if os.path.isdir(bin_dir):
os.add_dll_directory(bin_dir)
from PyQt5 import QtCore
if {environment == "conda"}: # conda-forge linux, macos and windows
import PyQt5._cx_freeze_resource
elif {IS_MACOS}: # macos using 'pip install pyqt5'
# Support for QtWebEngine (bdist_mac differs from build_exe)
helpers = os.path.join(os.path.dirname(sys.frozen_dir), "Helpers")
helpers = os.path.join(os.path.dirname(frozen_dir), "Helpers")
if not os.path.isdir(helpers):
helpers = os.path.join(sys.frozen_dir, "share")
helpers = os.path.join(frozen_dir, "share")
os.environ["QTWEBENGINEPROCESS_PATH"] = os.path.join(
helpers,
"QtWebEngineProcess.app/Contents/MacOS/QtWebEngineProcess"
Expand All @@ -92,7 +102,18 @@ def load_pyqt5(finder: ModuleFinder, module: Module) -> None:
else:
# Support for QtWebEngine (linux and windows using pip)
os.environ["QTWEBENGINE_DISABLE_SANDBOX"] = "1"
import PyQt5._cx_freeze_append_to_init
# With PyQt5 5.15.4, if the folder name contains non-ascii characters,
# the libraryPaths returns empty.
# Prior to this version, this doesn't happen.
plugins_dir = os.path.join(qt_root_dir, "Qt5", "plugins") # 5.15.4
if not os.path.isdir(plugins_dir):
plugins_dir = os.path.join(qt_root_dir, "Qt", "plugins")
if not os.path.isdir(plugins_dir):
plugins_dir = os.path.join(qt_root_dir, "plugins")
if os.path.isdir(plugins_dir):
QtCore.QCoreApplication.addLibraryPath(
plugins_dir.replace(os.path.sep, os.path.altsep or "/")
)
import PyQt5._cx_freeze_debug
# cx_Freeze patch end
"""
Expand Down
23 changes: 0 additions & 23 deletions cx_Freeze/hooks/pyqt5/_append_to_init.py

This file was deleted.

0 comments on commit 586bef5

Please sign in to comment.