PyInstaller——一个文件抛出带有panda、matplotlib和sklearn的Qt错误



使用带有--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

但是输出的14MB.exe运行良好,并显示一个Qt窗口。然而,当我尝试添加panda、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:UsersnameAppDataLocalTemp_MEI75002Includepyconfig.h
Traceback (most recent call last):
  File "<string>", line 2 in <module>
  File "PYINSTALLERDIRPyInstallerloaderpyi_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:UsersnameAppDataLocalTemp_MEI85162includepyconfig.h
Sub class of QObject not inheriting QObject!? Crash will happen when using Example.

我已经尝试过PyInstaller2.0和dev版本。当使用默认的--onedir而不是--onefile时,这三种场景都能很好地工作。有人能帮我找出使用--onefile时出现的问题吗?

更新:我尝试在PyInstaller2.1中使用panda进行构建,但在使用--onefile时仍然会遇到同样的错误。同样,当不使用--onefile时,一切都正常。

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

看完这篇文章后,我尝试在我的规范文件中添加'PySide'作为排除,以强制独占使用PyQt4,exe现在运行良好。您列出的模块应该可以很好地与PyQt4配合使用,因此它也有望解决您的问题。

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

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

最新更新