【问题标题】:Creating an OSX PyQt app using Pyinstaller 2, PyQt4 and Qt5使用 Pyinstaller 2、PyQt4 和 Qt5 创建 OSX PyQt 应用程序
【发布时间】:2013-03-27 05:10:45
【问题描述】:

我正在尝试使用 PyInstaller 2 为 OSX 打包 PyQt 程序,其中 PyQt4 (4.10) 是针对 Qt 5.0.2(来自 Git)构建的。以下简单示例不起作用。

import sys
from PyQt4.QtGui import QApplication, QMessageBox

def main():
    print "Hello"
    a = QApplication(sys.argv)
    m = QMessageBox(QMessageBox.Information, "Title", "Hello")
    m.exec_()

if __name__=="__main__":
    main()

使用 pyinstaller-2.0/utils/MakeSpec.py 生成的规范文件并修改以添加 BUNDLE 类。

a = Analysis(['hello.py'],
         pathex=['/Users/glenn/rp/src/demo'],
         hiddenimports=[],
         hookspath=None)

pyz = PYZ(a.pure)

exe = EXE(pyz,
      a.scripts,
      exclude_binaries=1,
      name=os.path.join('build/pyi.darwin/hello', 'hello'),
      debug=False,
      strip=None,
      upx=True,
      console=False )

coll = COLLECT(exe,
           a.binaries,
           a.zipfiles,
           a.datas,
           strip=None,
           upx=True,
           name=os.path.join('dist', 'hello'))

app = BUNDLE(coll,
        name=os.path.join('dist', 'hello.app'),
        appname="Hello",
        version = '0.1'
        )

打包命令

> python pyinstaller.py --windowed hello.spec

直接从终端运行二进制文件会在崩溃之前给出以下输出:

$ ./dist/hello.app/Contents/MacOS/hello 
Hello
Failed to load platform plugin "cocoa". Available platforms are: 

Abort trap: 6

这是堆栈跟踪:

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000

Application Specific Information:
abort() called

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib          0x9a671a6a __pthread_kill + 10
1   libsystem_c.dylib               0x93163b2f pthread_kill + 101
2   libsystem_c.dylib               0x9319a4ec abort + 168
3   QtCore                          0x03db156b qt_message_fatal(QtMsgType, QMessageLogContext const&, QString const&) + 11
4   QtCore                          0x03db19df QMessageLogger::fatal(char const*, ...) const + 63
5   QtGui                           0x068abceb QGuiApplicationPrivate::createPlatformIntegration() + 3547
6   QtGui                           0x068abd16 QGuiApplicationPrivate::createEventDispatcher() + 38
7   QtCore                          0x03f4f2c4 QCoreApplication::init() + 100
8   QtCore                          0x03f4f23b QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) + 59
9   QtGui                           0x068aa0d0 QGuiApplication::QGuiApplication(QGuiApplicationPrivate&) + 32
10  QtWidgets                       0x06c695de QApplication::QApplication(int&, char**, int) + 238
11  PyQt4.QtGui.so                  0x06394454 init_QApplication + 196
12  sip.so                          0x007bc7d5 sipSimpleWrapper_init + 266
13  Python                          0x0385c174 type_call + 340
14  Python                          0x0380d401 PyObject_Call + 97
15  Python                          0x0389c093 PyEval_EvalFrameEx + 10131
16  Python                          0x038a0490 fast_function + 192
17  Python                          0x0389beae PyEval_EvalFrameEx + 9646
18  Python                          0x038998b2 PyEval_EvalCodeEx + 1922
19  Python                          0x03899127 PyEval_EvalCode + 87
20  Python                          0x038be06e PyRun_StringFlags + 126
21  Python                          0x038bdfb1 PyRun_SimpleStringFlags + 81
22  Python                          0x038bf619 PyRun_SimpleString + 25
23  hello                           0x00003240 runScripts + 240
24  hello                           0x0000280a main + 442
25  hello                           0x00001eb9 _start + 224
26  hello                           0x00001dd8 start + 40

问题似乎是它找不到 libqcocoa.dylib 插件。这并不奇怪,因为它没有打包。这是这里的实际问题吗?我需要包含这个插件吗?如果是这样,它需要去哪里?我已经尝试将它放在 demo.app/Contents/plugins 中,但这没有帮助。

【问题讨论】:

  • 我在 OSX 10.7.5 上尝试使用 Qt 4.8.1、Pyside 1.1.1、Pyinstaller 的最新开发版本,但没有对规范进行修复,它也崩溃了。我无法测试您对规范的修复,因为我的计算机上不存在 libqcocoa.dylib? PySide 和 PyQt 的区别?同时,在您关闭应用程序之前,打开主窗口的稍大一点的程序不会崩溃。
  • libqcocoa 的问题是 Qt5 特有的。

标签: macos pyqt qt5 pyinstaller


【解决方案1】:

libqcocoa.dylib 的正确放置位置在Contents/MacOS/qt4_plugins/platforms:

extralibs = [("qt4_plugins/platforms/libqcocoa.dylib", "/path/to/libqcocoa.dylib", "BINARY")
             ]
coll = COLLECT(exe,
               a.binaries + extralibs,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=False,
               name=os.path.join('dist', 'hello'))

应用程序现在启动,但对话窗口从未出现。编辑:这是因为 PyInstaller (v 2.0) 将 LSBackgroundOnly=true 添加到应用程序 Info.plist。删除它可以显示窗口。

【讨论】:

  • 我遇到了类似的问题,原因是我必须更改一些路径,具体取决于我是否从 pyinstaller 运行。将 LSBackgroundOnly 设置为 False 会使我的 Dock 中出现两个图标
  • 这里有一个关于 LSBackgroundOnly 和双图标问题的讨论groups.google.com/forum/#!topic/pyinstaller/f3800Jc0rr0
【解决方案2】:

尝试添加(但针对 PyQt 进行了更改):

from PySide import QtCore, QtGui

至少这对我有用(PySide 1.1.1、Qt 4.8.1、最新开发的 Pyinstaller、OSX 10.7.5,没有更改规范文件和 pyinstaller 的 --onefile --windowed 参数。)并且没有这个sn-p,我也崩溃了。

我想这会带来更多的依赖。

在另一个答案中找到了这个。

【讨论】:

  • 这个问题是 Qt5 特有的。
猜你喜欢
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
相关资源
最近更新 更多