我处理了同样的问题,最后切换到cx_freeze,现在在Linux和Windows上都可以正常工作。您正在处理的问题源于您发现的SE答案之类的语句,即动态导入语句,其中导入的内容仅在运行时确定:
be = 'pyface.ui.%s.' % tk __import__(be + 'init')
当您在构建文件中明确添加所需的软件包时,我无法在pyinstaller中修复该问题,而在cx_freeze中它可以工作。这是我使用的软件包列表:
"packages": ["pyface.ui.qt4", "tvtk.vtk_module", "tvtk.pyface.ui.wx", "matplotlib.backends.backend_qt4",'pkg_resources._vendor','pkg_resources.extern','pygments.lexers', 'tvtk.pyface.ui.qt4','pyface.qt','pyface.qt.QtGui','pyface.qt.QtCore','numpy','matplotlib','mayavi']
这是一个完整的构建脚本,可与python3.6,cx_freeze 5.0.2,mayavi 4.5.0 + vtk71,特征4.6.0,pyface
5.1.0和traitsui 5.1.0一起使用。
import osfrom cx_Freeze import setup, Executableimport cx_Freeze.hooksdef hack(finder, module): returncx_Freeze.hooks.load_matplotlib = hackimport scipyimport matplotlibscipy_path = os.path.dirname(scipy.__file__) #use this if you are also using scipy in your applicationbuild_exe_options = {"packages": ["pyface.ui.qt4", "tvtk.vtk_module", "tvtk.pyface.ui.wx", "matplotlib.backends.backend_qt4",'pygments.lexers', 'tvtk.pyface.ui.qt4','pyface.qt','pyface.qt.QtGui','pyface.qt.QtCore','numpy','matplotlib','mayavi'], "include_files": [(str(scipy_path), "scipy"), #for scipy (matplotlib.get_data_path(), "mpl-data"),], "includes":['PyQt4.QtCore','PyQt4.QtGui','mayavi','PyQt4'], 'excludes':'Tkinter', "namespace_packages": ['mayavi'] }executables = [ Executable('main.py', targetName="main.exe",base = 'Win32GUI',)]setup(name='main', version='1.0', description='', options = {"build_exe": build_exe_options}, executables=executables, )我通过以下方式导入pyface:
os.environ['ETS_TOOLKIT'] = 'qt4'import imptry: imp.find_module('PySide') # test if PySide if availableexcept importError: os.environ['QT_API'] = 'pyqt' # signal to pyface that PyQt4 should be usedfrom pyface.qt import QtGui, QtCore导入mayavi之前



