无法使用 cx_freeze 和 PySide2 进行编译
Posted
技术标签:
【中文标题】无法使用 cx_freeze 和 PySide2 进行编译【英文标题】:Unable to compile with cx_freeze and PySide2 【发布时间】:2019-07-21 16:46:56 【问题描述】:我有一个 python 程序,我正在尝试用 cx_freeze 编译。我使用的 GUI 是 PySide2。
我试过包括 PySide2,这里不包括它,但我一直收到同样的错误。下面是我的 setup.py 代码
from cx_Freeze import setup, Executable
import sys
includefiles = ['README.md', 'debug.log','tcl86t.dll', 'tk86t.dll', 'field.jpg', 'inputClass.py', 'mainfile.qml', 'MyTabView.qml', 'PlayerSelection.qml', 'selectedPlayers.py', 'Settings.qml', 'SimOutput.qml', 'simulationOutput.py']
includes = ["idna.idnadata", "atexit"]
excludes = ["PySide2"]
import os
os.environ['TCL_LIBRARY'] = r'C:\Users\pimat\AppData\Local\Programs\Python\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\pimat\AppData\Local\Programs\Python\Python36\tcl\tk8.6'
setup(name = "Simulation",
version = "0.2",
description = "Optimization Simulator",
options = 'build_exe':'includes':includes,'excludes':excludes,'include_files':includefiles,
executables = [Executable("main.py")])
程序编译正常,但是运行exe时出现以下错误:
“ModuleNotFoundError: 没有名为‘PySide2’的模块”
【问题讨论】:
【参考方案1】:所以错误是我使用 python 3.6 安装了 cx_freeze,但我所有的包都在 python 3.7 文件夹中。我只是简单的复制粘贴到3.6文件夹中,稍微修改了一下代码,exe运行良好。
from cx_Freeze import setup, Executable
import sys
# dependencies
build_exe_options =
"packages": ["os", "sys", "re", "idna.idnadata", "atexit", "PySide2.QtCore", "PySide2.QtWidgets", "PySide2.QtUiTools", "PySide2.QtQuick", "PySide2.QtQml", "PySide2.QtGui", "shiboken2"],
"include_files": ['README.md', 'debug.log','tcl86t.dll', 'tk86t.dll', 'field.jpg', 'inputClass.py', 'mainfile.qml', 'MyTabView.qml', 'PlayerSelection.qml', 'selectedPlayers.py', 'Settings.qml', 'SimOutput.qml', 'simulationOutput.py',
],
"excludes": ["Tkinter", "Tkconstants", "tcl", ],
"build_exe": "build",
#"icon": "./example/Resources/Icons/monitor.ico"
executable = [
Executable("main.py",
base="Win32GUI",
targetName="Simulation.exe"
)
]
setup(name = "Simulation",
version = "0.2",
description = "Simulator",
options="build_exe": build_exe_options,
executables=executable
)
这是一个愚蠢的错误,但我做得更糟
【讨论】:
以上是关于无法使用 cx_freeze 和 PySide2 进行编译的主要内容,如果未能解决你的问题,请参考以下文章
带有 Pyside2 和 Matplotlib 的 Pyinstaller 无法正常工作
无法使用 virtualenv 和 cx_Freeze 将 pygame 转换为可执行文件
带有 Pyinstaller 的 PySide2:无法执行脚本 pyi_rth_pkgres