【问题标题】:PyInstaller --onefile throws Qt error with pandas, matplotlib and sklearnPyInstaller --onefile 使用 pandas、matplotlib 和 sklearn 引发 Qt 错误
【发布时间】:2013-09-01 23:01:00
【问题描述】:

使用带有 --onefile 标志的 PyInstaller,我可以成功地将以下脚本构建到 .exe 中:

import sys
from PyQt4 import QtGui

class Example(QtGui.QMainWindow):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):               
        self.statusBar().showMessage('Ready')
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Statusbar')    
        self.show()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我在构建时收到以下警告:(为了便于阅读,我使用“PYINSTALLERDIR”替换完整路径,即“C:\Users\name\Downloads\pyinstaller-pyinstaller-v2.0-544-g337ae69\pyinstaller -pyinstaller-337ae69\".

PYINSTALLERDIR>pyinstaller.py --onefile --log-level=WARN MainWindowHello.py
1306 WARNING: library python%s%s required via ctypes not found
1468 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
2957 WARNING: library python%s%s required via ctypes not found

但输出的 14 MB .exe 运行良好并显示 Qt 窗口。但是,当我尝试添加 pandas、matplotlib 或 sklearn 时,我遇到了 Qt 问题。

在脚本的第 3 行添加 import matplotlibimport sklearn 会在构建时给我以下警告:

PYINSTALLERDIR>python pyinstaller.py --onefile --log-level=WARN MainWindowHello.py
1371 WARNING: library python%s%s required via ctypes not found
1528 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
3051 WARNING: library python%s%s required via ctypes not found
27108 INFO: Adding Microsoft.VC90.MFC to dependent assemblies of final executable
33329 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable

当我尝试运行生成的 .exe(matplotlib 为 44 MB,sklearn 为 87 MB)时,没有显示 Qt 窗口并且我收到以下错误消息:

WARNING: file already exists but should not: C:\Users\name\AppData\Local\Temp\_MEI75002\Include\pyconfig.h
Traceback (most recent call last):
  File "<string>", line 2 in <module>
  File "PYINSTALLERDIR\PyInstaller\loader\pyi_importers.py", line 409, in load_module
ImportError: could not import module 'PySide.QtCore'

在第 3 行使用import pandas,我得到了相同的警告(以及关于 libzmq.pyd 的警告,但我之前已经通过工作程序得到了它们)。当我尝试运行 119 MB 的 .exe 时,程序崩溃并抛出以下错误:

WARNING: file already exists but should not: C:\Users\name\AppData\Local\Temp\_MEI85162\include\pyconfig.h
Sub class of QObject not inheriting QObject!? Crash will happen when using Example.

我已经尝试过 PyInstaller 2.0 和开发版本。使用默认的 --onedir 而不是 --onefile 时,所有三种方案都可以正常工作。谁能帮我弄清楚使用 --onefile 时出了什么问题?

更新:我尝试在 PyInstaller 2.1 中使用 pandas 进行构建,但在使用 --onefile 时仍然遇到相同的错误。同样,不使用 --onefile 时一切正常。

【问题讨论】:

    标签: matplotlib pandas pyqt4 scikit-learn pyinstaller


    【解决方案1】:

    我在导入 PyQt4 的脚本以及导入 PySide 的一些模块时遇到了同样的问题。 PyInstaller 在使用 --onedir 选项(默认)时运行良好,但在使用 --onefile 选项时我得到了 ImportError: could not import module 'PySide.QtCore'

    阅读this 后,我尝试在我的规范文件中添加'PySide' 作为排除项,以强制独占使用PyQt4,exe 现在运行良好。您列出的模块应该可以在 PyQt4 上正常工作,因此它也有望解决您的问题。

    此外,虽然这不是一个大问题,但 here 描述了 file already exists 警告的解决方案。只需在 a = Analysis... 之后的规范文件中添加这些行即可删除导致警告的重复项:

    for d in a.datas:
        if 'pyconfig' in d[0]: 
            a.datas.remove(d)
            break
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-09
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      相关资源
      最近更新 更多