使用 cx_freeze 冻结后的绝对路径(Qt5 / PySide2 App)

Absolute paths after freezing with cx_freeze (Qt5 / PySide2 App)

问题:

我无法将 cx_freeze 生成的 .exe 分发到另一台机器,因为该 exe 似乎包含对生成该 .exe 的机器上的绝对路径的引用。我还必须直接包含 vcruntime140.dll,因为 "include_msvcr": True 没有复制文件。

设置

类似问题

这个问题以前曾以类似的形式提出过,但没有答案:cx_Freeze copies path

错误日志

启动脚本时,出现以下错误(无法从 window copy/paste,所以我分享了一张图片)。您可以看到对绝对路径 C:\Program Files (x86)\Python.... 的引用,这显然不存在于另一台机器上。

冻结脚本

from os.path import dirname
from cx_Freeze import setup, Executable
from config import settings
import os.path
import sys
import glob

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
DEPENDENCY_DIR = os.path.join(os.getcwd(), 'dependencies')
os.environ['TCL_LIBRARY'] = os.path.join(DEPENDENCY_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(DEPENDENCY_DIR, 'tcl', 'tk8.6')

packages = ["sepa", "datev", "atexit", "shiboken2", "PySide2"]
includes = []
excludes = ["Pyside2.Qt5WebEngineCore.dll"]
includefiles = ['qt', 'settings', 'config', os.path.join(DEPENDENCY_DIR, 'DLLs', 'tk86t.dll'),
             os.path.join(DEPENDENCY_DIR, 'DLLs', 'tcl86t.dll'), os.path.join(DEPENDENCY_DIR, 'DLLs', 'vcruntime140.dll')]

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": packages,
                     "excludes": excludes,
                     "includes": includes,
                     "include_files": includefiles,                     
                     "optimize": 2,
                     "include_msvcr": True}



# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(name="sepa_converter",
      version=settings.version,
      description="Programm zum Konvertiern von SEPA Dateien zum importieren in Buchhaltungsprogramme",
      options={"build_exe": build_exe_options},
      executables=[Executable("export_gui.py", base=base)])

#Debug DLLs von Pyside2 löschen
tmp = glob.glob("build/*/Pyside2/*d.dll")
for i in tmp:
    os.remove(i)

tmp = glob.glob("build/*/Pyside2/*/*/*d.dll")
for i in tmp:
    os.remove(i)

filelist = ['Qt5WebEngineCore.dll', 'icudt54.dll', 'opengl32sw.dll', 'Qt5Designer.dll', 'd3dcompiler_47.dll', 'Qt5Quick.dll']
for f in filelist:
    for i in glob.glob("build/*/Pyside2/%s" % f):
        os.remove(i)

降级到 cx_freeze 5.1.1 解决了我的问题。

实现它的步骤:

  1. 安装Python 3.6 32 位。其他版本对我不起作用。 32 位似乎对脚本的 Qt / Tk 部分很重要,3.6 对 cx-freeze 5.1.1 很重要。
  2. 安装cx-freeze==5.1.1
  3. 生成 .exe。