PyInstaller --onefile使用pandas,matplotlib和sklearn抛出Qt错误

时间:2013-08-29 13:19:27

标签: matplotlib pandas pyqt4 scikit-learn pyinstaller

使用PyInstaller和--onefile标志,我可以成功地将以下脚本构建到.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和dev版本。使用默认的--onedir而不是--onefile时,所有三种方案都能正常工作。任何人都可以帮我弄清楚使用--onefile时会出现什么问题吗?

更新:我尝试在PyInstaller 2.1中使用pandas进行构建,并且在使用--onefile时仍然会出现相同的错误。同样,当不使用--onefile时,一切都有效。

1 个答案:

答案 0 :(得分:1)

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

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

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

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