该应用程序无法启动,因为它无法找到或加载Qt平台插件"cocoa". [英] This application failed to start because it could not find or load the Qt platform plugin "cocoa"

查看:450
本文介绍了该应用程序无法启动,因为它无法找到或加载Qt平台插件"cocoa".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我在过去20个小时中尽了一切努力,但似乎没有任何效果.我的应用程序正在运行并且正在工作-正如它应该-那样,我唯一的问题是我无法从中创建.app捆绑包.我同时尝试了Py2Appcx_Freeze,但是它们都不起作用.由于有多平台支持,因此我会坚持使用后者-如果可能的话.

I think I did everything I could in the last 20 hours, but nothing seems to work. My app is running and working -- just as it should -- the only problem I have is that I cannot create a .app bundle from it. I tried both Py2App and cx_Freeze but non of them is working. Because of the multi-platform support I would stick with the latter -- if possible.

setup.py看起来像这样:

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

OPTIONS = {'build_exe': {'includes': ['sip',
                                      'PyQt5',
                                      'PyQt5.QtCore',
                                      'PyQt5.QtGui',
                                      'PyQt5.QtWidgets',
                                      'PyQt5.QtMultimediaWidgets',
                                      'PyQt5.QtMultimedia',
                                      'PyQt5.QtNetwork']}}

EXECUTABLES = [Executable('main.py', base=base)]
NAME = 'coublet'
VERSION = '0.5.70'

setup(name = NAME,
      version = VERSION,
      options = OPTIONS,
      executables = EXECUTABLES)

我收到的错误消息是这样的:

The error message I have is this:

objc[28404]: Class NotificationReceiver is implemented in both
/Users/.../build/coublet-0.5.70.app/Contents/MacOS/QtWidgets and
/usr/local/Cellar/qt5/5.3.1/lib/QtWidgets.framework/Versions/5/QtWidgets. One of
the two will be used. Which one is undefined.
QObject::moveToThread: Current thread (0x7fc4b96e98b0) is not the object's thread
(0x7fc4b95dbc80).
Cannot move to target thread (0x7fc4b96e98b0)

On Mac OS X, you might be loading two sets of Qt binaries into the same process.
Check that all plugins are compiled against the right Qt binaries. Export
DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.
This application failed to start because it could not find or load the Qt
platform plugin "cocoa".

Available platform plugins are: cocoa, minimal, offscreen.

Reinstalling the application may fix this problem.
Abort trap: 6

我的系统信息:

Mac OS X  : 10.9.4
Python    :  3.4.1
cx_Freeze :  0.9
PyQt5:    :  5.3.1
- - -
Packages installed via: Homebrew and PIP

.app结构:

build/coublet-0.5.70.app
└── Contents
    ├── Frameworks
    ├── Info.plist
    ├── MacOS
    │   ├── PyQt5.QtCore.so
    │   ├── PyQt5.QtGui.so
    │   ├── PyQt5.QtMultimedia.so
    │   ├── PyQt5.QtMultimediaWidgets.so
    │   ├── PyQt5.QtNetwork.so
    │   ├── PyQt5.QtWidgets.so
    │   ├── Python
    │   ├── QtCore
    │   ├── QtCore.so
    │   ├── QtGui
    │   ├── QtGui.so
    │   ├── QtMultimedia
    │   ├── QtMultimedia.so
    │   ├── QtMultimediaWidgets
    │   ├── QtMultimediaWidgets.so
    │   ├── QtNetwork
    │   ├── QtNetwork.so
    │   ├── QtOpenGL
    │   ├── QtWidgets
    │   ├── QtWidgets.so
    │   ├── _bisect.so
    │   ├── _bz2.so
    │   ├── _codecs_cn.so
    │   ├── _codecs_hk.so
    │   ├── _codecs_iso2022.so
    │   ├── _codecs_jp.so
    │   ├── _codecs_kr.so
    │   ├── _codecs_tw.so
    │   ├── _datetime.so
    │   ├── _hashlib.so
    │   ├── _heapq.so
    │   ├── _json.so
    │   ├── _lzma.so
    │   ├── _md5.so
    │   ├── _multibytecodec.so
    │   ├── _opcode.so
    │   ├── _pickle.so
    │   ├── _posixsubprocess.so
    │   ├── _random.so
    │   ├── _scproxy.so
    │   ├── _sha1.so
    │   ├── _sha256.so
    │   ├── _sha512.so
    │   ├── _socket.so
    │   ├── _ssl.so
    │   ├── _struct.so
    │   ├── array.so
    │   ├── binascii.so
    │   ├── grp.so
    │   ├── imageformats
    │   │   ├── libqdds.dylib
    │   │   ├── libqgif.dylib
    │   │   ├── libqicns.dylib
    │   │   ├── libqico.dylib
    │   │   ├── libqjp2.dylib
    │   │   ├── libqjpeg.dylib
    │   │   ├── libqmng.dylib
    │   │   ├── libqsvg.dylib
    │   │   ├── libqtga.dylib
    │   │   ├── libqtiff.dylib
    │   │   ├── libqwbmp.dylib
    │   │   └── libqwebp.dylib
    │   ├── libcrypto.1.0.0.dylib
    │   ├── liblzma.5.dylib
    │   ├── library.zip
    │   ├── libreadline.6.dylib
    │   ├── libssl.1.0.0.dylib
    │   ├── main
    │   ├── math.so
    │   ├── platforms
    │   │   ├── libqcocoa.dylib
    │   │   ├── libqminimal.dylib
    │   │   └── libqoffscreen.dylib
    │   ├── pyexpat.so
    │   ├── readline.so
    │   ├── select.so
    │   ├── sip.so
    │   ├── termios.so
    │   ├── time.so
    │   ├── unicodedata.so
    │   └── zlib.so
    └── Resources

我认为问题很明显:我做错了什么? (或者我在做什么?)

The question is I think pretty obvious: what am I doing wrong? (or what am I not doing?)

推荐答案

在MacOSX上使用cx_Freeze构建应用程序时,所有依赖库(MacOSX上的.so文件)都打包到应用程序捆绑包中.正是这样,使该应用程序可移植到其他系统,而无需第二次安装Qt.

When building an app with cx_Freeze on MacOSX all dependent libraries (.so files on MacOSX) are packaged into the application bundle. It is this that makes the application portable to other systems, without requiring a second install of Qt.

启动应用程序时,因此应从捆绑软件中加载库.但是,根据您的情况,系统库仍在加载:

When launching the Application, the libraries should therefore be loaded from within the bundle. However, in your case the system libraries are still being loaded:

/Users/.../build/coublet-0.5.70.app/Contents/MacOS/QtWidgets
/usr/local/Cellar/qt5/5.3.1/lib/QtWidgets.framework/Versions/5/QtWidgets

结果One of the two will be used. Which one is undefined.表示可以加载其中任何一个.如果选择正确的一个,那就太好了!如果没有,那么您将同时加载两组独立的库,此后不久就会失败.顺便说一句,您可能会发现,如果您在另一个系统上尝试您的应用程序,它将可以正常工作! 有时.

The result One of the two will be used. Which one is undefined. means that either of these could be loaded. If it picks the correct one, great! If it doesn't you've got two separate sets of libraries loading simultaneously, and that fails shortly afterwards. As an aside, you may find that if you try your Application on another system it will work fine! Sometimes.

有关该问题的概述,建议您查看以下错误#33 .

For an overview of the problem I suggest taking a look at the following bug #33.

确保已安装 cx_Freeze 的最新版本.我建议尝试克隆存储库并从那里安装.

Make sure you have an up-to-date version of cx_Freeze installed. I would suggest trying to clone the repository and installing from there.

第二,确保Qt插件已正确构建到您的应用程序中. Cx_Freeze以前在qt-menu.nib文件中查找,以确定它是否在构建Qt应用程序. Qt5中不再提供此功能,但是您可以在构建应用程序时在命令行中传递它.将其设置为您想要的任何东西,真的没关系:

Secondly, ensure that the Qt plugins are being correctly built into your application. Cx_Freeze previously looked for the qt-menu.nib file to determine if it was building a Qt application. This is no longer available in Qt5, but you can pass it on the command-line when building your app. Set it to whatever you want, it really doesn't matter:

python setup.py bdist_mac --qt-menu-nib=/usr/local/Cellar/qt5/5.3.1/plugins/platforms/

这可能足以解决您的问题.但是,如果没有,您有两种选择:

This may be enough to fix your problem. But if not, you have two options:

每个库文件都包含其依赖项的路径.如果收到此错误,则意味着其中一些路径要么是a)仍指向原始文件,要么是b)不够具体(并在PATHDYLD_LIBRARY_PATH上找到).但是,您可以从命令行使用install_name_tool重新写入路径(如

Each library file contains the paths to it's dependencies. If you're receiving this error, it means some of those paths are either a) still pointing to the original file, or b) not specific enough (and being found on your PATH or DYLD_LIBRARY_PATH). However, you can re-write paths using install_name_tool from the command line (as described here:

install_name_tool -change /usr/local/Cellar/qt5/5.3.1/lib/QtWidgets.framework/Versions/5/QtWidgets @executable_path/QtWidgets build/MyApp.app/Contents/MacOS/qt_plugins/platforms/libqcocoa.dylib
install_name_tool -change /usr/local/Cellar/qt5/5.3.1/lib/QtCore.framework/Versions/5/QtCore @executable_path/QtCore build/MyApp.app/Contents/MacOS/qt_plugins/platforms/libqcocoa.dylib
install_name_tool -change /usr/local/Cellar/qt5/5.3.1/lib/QtPrintSupport.framework/Versions/5/QtPrintSupport @executable_path/QtPrintSupport build/MyApp.app/Contents/MacOS/qt_plugins/platforms/libqcocoa.dylib
install_name_tool -change /usr/local/Cellar/qt5/5.3.1/lib/QtGui.framework/Versions/5/QtGui @executable_path/QtGui build/MyApp.app/Contents/MacOS/qt_plugins/platforms/libqcocoa.dylib

使用@executable_path作为基础,重写库中的路径以指向您的应用程序文件夹.您将需要为发现错误加载的所有路径执行此操作.我建议将其包装到脚本中,以在构建后自动运行.

This rewrites the paths in the libraries to point into your application folder, using @executable_path as the base. You will need to do this for all the paths that you find loading incorrectly. I'd suggest wrapping it up into an script, to run automatically after the build.

如果要查看文件正在引用的库,可以使用otool.例如,在我的成功构建的应用程序中:

If you want to look at which libraries a file is referencing you can use otool. For example, in a successfully built application of mine:

otool -L libqcocoa.dylib 
libqcocoa.dylib:
    @executable_path/../Resources/qt_plugins/platforms/libqcocoa.dylib (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 20.0.0) 
...

有一个问题跟踪器,它表明只需在应用程序中导入正确的模块即可使其正常运行,但是,编写不带QtWidgets的应用程序的可能性似乎很小.

There was an updated workaround in the issue tracker that suggests that just importing the correct module in your app will get it to function, however it seems unlikely that you've made an application without QtWidgets.

如果上述方法无效,请在此处中概述另一种方法.这有点像大锤,因为它根本就阻止了插件的全部加载.

If the above doesn't work, there is another approach outlined here. This is a bit of a sledgehammer approach in that it simply prevents loading of plugins at all.

  • 在可执行文件旁边添加一个qt.conf文件(在.app捆绑软件中,该文件包含:
    [Paths] Plugins = '.'

  • Add a qt.conf file next to the executable (in the .app bundle that contains:
    [Paths] Plugins = '.'

要么设置环境变量QT_PLUGIN_PATH=""(您可以在导入PyQt之前在应用程序中执行此操作.或者在创建应用程序对象之前调用QtGui.QApplication.setLibraryPaths([]).

Either set the environment variable QT_PLUGIN_PATH="" (you can do this within your application before importing PyQt. Or call QtGui.QApplication.setLibraryPaths([]) before creating your application object.

结果是没有插件,因此您的应用程序将无法访问MacOSX Cocoa样式和UI(例如文件,颜色对话框).

The result is no plugins, so your application will not have access to the MacOSX Cocoa style and UI (e.g. file, colour dialogs).

这篇关于该应用程序无法启动,因为它无法找到或加载Qt平台插件"cocoa".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆