【问题标题】:PyInstaller error with PyQt when trying to build --onefile尝试构建 --onefile 时 PyQt 出现 PyInstaller 错误
【发布时间】:2012-01-22 20:23:38
【问题描述】:

我正在尝试使用 PyInstaller 1.5 编译 PyQt 程序。当我使用 --onedir (默认值)时,以下两个程序对我来说都很好,但这会创建相当大的程序。我想使用 --onefile 选项,但是当我运行创建的 onefile 应用程序时,我收到错误:

Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "pyinstaller/PyInstaller/loader/iu.py", line 468, in importHook
raise ImportError("No module named %s" % fqname)
ImportError: No module named PyQt4.QtCore

这两种情况都会出现此错误:

import sys 
from PyQt4 import QtCore, QtGui 

app =QtGui.QApplication(sys.argv) 
window =QtGui.QMainWindow() 
window.setCentralWidget(QtGui.QLabel("Hello")) 
window.show() 
sys.exit(app.exec_()) 

还有这个:

import sys
import PyQt4.QtCore, PyQt4.QtGui 

app = PyQt4.QtGui.QApplication(sys.argv) 
window = PyQt4.QtGui.QMainWindow() 
window.setCentralWidget(PyQt4.QtGui.QLabel("Hello")) 
window.show() 
sys.exit(app.exec_()) 

有人有什么想法吗?

【问题讨论】:

    标签: python pyqt pyinstaller


    【解决方案1】:

    适用于我(Windows 7x64bit,Python 2.7x32bit)只需将 QT 目录添加到您的系统路径或使用 p 选项将其添加到命令行:

    PyInstaller -y -F --distpath="." -p "C:\Python27\Lib\site-packages\PyQt4" test.py
    

    如果您从可执行文件安装 PyQt,它会自动为您完成所有这些工作:

    http://sourceforge.net/projects/pyqt/files/

    【讨论】:

    • 不知何故 pyinstaller 没有自动包含 PySide 库。不过,您的解决方案对我有用。但是还有其他解决方法吗?
    【解决方案2】:

    1,Pyinstaller 不会创建比 --onedir 更小的 --onefile。当您运行 --onefile 时,它​​只是创建一个包装器,将目录中的所有内容提取到一个临时目录,然后运行它。

    2、Pyinstaller不支持import PyQt4.QtCore, PyQt4.QtGui,根据herefrom PyQt4 import QtCore, QtGui是唯一支持的方式。

    3,你的 PyQt4 是什么版本的? Riverbank 的安装程序是 GPL 版本吗?

    4,您是否正确执行了这些步骤?例如Makespec.py 然后Build.py?

    【讨论】:

    • 我试图找到我的 PyQt 构建示例 .spec 文件,我在 linux 机器上做了一段时间。我找不到它,但我认为我必须对规范文件中的 Analysis() 定义做一些事情。也许我错了,我所做的只是 from PyQt4 import QtCore, QtGui 导入语法。
    • 事实上,您可以随时将PyQt4 添加到规范文件中,这将确保pyinstaller 将包含它。
    • 您介意告诉我如何将 PyQt4(或任何其他大包)添加到规范文件吗?恐怕文档不够清楚,我无法理解我在做什么。
    • 我正在运行最新的主干版本的 PyInstaller,我正在运行最新的 MacPorts 版本的 PyQt。我正在使用之前生成的 .spec 文件运行 Build.py。
    • 我终于选择了 --onedir,因为正如@FelixYan 所提到的,从长远来看,它不会节省任何硬盘空间(并且因为您必须提取程序的内容而减慢了速度每一次)。谢谢!
    【解决方案3】:

    我在使用 pyinstaller 3.4(python 3.6,PyQt5)时遇到了同样的问题,最后通过调整 herehere 中的解决方案使其正常工作。

    方法总结:

    1) 运行“pip install pip==18.1”。尽管也存在较新的版本,但使用 18.1 版本至关重要。完成此过程后,您可以通过“pip install pip --upgrade”升级 pip

    2) 安装pyinstaller开发版:“pip install https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz

    3 ) 正常运行pyinstaller

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2015-12-04
      • 2021-03-05
      • 1970-01-01
      • 2023-03-07
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2014-05-15
      相关资源
      最近更新 更多