ChatGPT解决这个技术问题 Extra ChatGPT

如何在 PyCharm 中使用 Matplotlib 时修复“找不到或无法加载 Qt 平台插件窗口”

在 PyCharm 中使用 matplotlib 时出现错误“无法找到或加载 Qt 平台插件窗口”。

我该如何解决这个问题?

https://i.stack.imgur.com/XXw0M.png

看看:stackoverflow.com/questions/20495620/…。否则,请尝试消息中的建议:“重新安装应用程序可能会解决此问题。”
不要使用屏幕截图向我们展示错误或代码。 “Discourage screenshots of code and/or errors
@Pawel 解决方案是正确的,或者您可以尝试 import matplotlib matplotlib.use('TKAgg')
问题在于 anaconda 和 matplotlib,使用普通 python,这是一个已知问题,请在此处查看 forum.qt.io/topic/133862/pyside6-does-not-work-with-anaconda

t
the Tin Man

我对 Anaconda3 4.2.0 和 4.3.0.1(64 位)也有同样的问题。当我尝试运行一个使用 matplotlib 的简单程序时,我收到了以下错误消息:

This application failed to start because it could not find or load the Qt platform plugin "windows"

Reinstalling the application may fix this problem.

重新安装并没有解决它。

这有什么帮助(找到 here):查找 Anaconda 目录并将 Library\plugins 子目录(此处为 c:\ProgramData\Anaconda3\Library\plugins)设置为控制面板/系统/高级系统设置/环境变量下的环境变量 QT_PLUGIN_PATH

设置变量后,如果更改没有立即生效,您可能需要重新启动 PyCharm。

即使在那之后命令行 Python 工作了,TexWorks(它也使用 Qt)显示了一条非常相似的错误消息。将 QT_PLUGIN_PATH 设置为包含 TexWorks 的 Qt DLL 的目录(此处为 C:\Users\chris\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64)解决了这两个程序的问题。


伟大的修复!但是这个修复对另一个我也需要 Qt 的软件产生了同样的错误;所以我每次都必须重新创建它。不知道有没有办法避免这种情况。
它适用于 Python,但建议不要将其添加到环境变量中,因为它会破坏其他系统。现在每次我打开我的电脑,Dropbox 都会抱怨缺少 Qt。很高兴知道这个问题的正确答案!
@AnttiA 2 年后,如果我修改环境变量,我会遇到确切的问题 - 问题本身已修复,但 Dropbox 不再正常工作。你能找到解决这个问题的方法吗?
t
the Tin Man

如果您正在运行 PyQt5 和 PySide2,这为我解决了问题:

复制以下文件:

\Anaconda3\Lib\site-packages\PySide2\plugins\platforms\qminimal.dll
\Anaconda3\Lib\site-packages\PySide2\plugins\platforms\qoffscreen.dll
\Anaconda3\Lib\site-packages\PySide2\plugins\platforms\qwindows.dll

至:

\Anaconda3\Library\plugins\platforms\

你能详细说明你是如何解决这个问题的吗?这个问题从何而来?您的解决方案非常有用,我想了解原因。 ^^
t
the Tin Man

如果您想以另一种方式可视化您的 matplotlib,请使用不同的后端来生成图形、图表等。

import matplotlib
matplotlib.use('TKAgg')

这对我有用。


这回避了问题,但鉴于 matplotlib 的灵活性,这对于一些不应被忽视的人来说是一个有用的解决方案!
伟大的。除了这个之外,这里没有任何解决方案有效!
R
Richard Ayling

我发现这是由于我的 PATH 变量中有 MiKTeX 二进制文件造成的;并且发现了错误的 Qt dll。我只需要重新排列 PATH 条目。

Dependency Walker 是一个非常有用的工具。)


t
the Tin Man

我在 Anaconda 的提示下尝试了以下操作,它解决了这个问题:

conda remove qt
conda remove pyqt 
conda install qt 
conda install pyqt

这修复了我的 Conda 环境。删除 qt 也删除了所有依赖它的库,所以我不得不重新安装它们。
这在我尝试启动 labelImg 时遇到 This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. 时有效。谢谢!
这对我有用,但是如果您想避免重新安装所有依赖包,请不要卸载任何东西,只需强制重新安装:conda install --force-reinstall pyqt conda install --force-reinstall qt
l
launchpadmcquack

我在 PyCharm 中遇到了类似的问题,在主运行中运行良好,但在调试器中却没有,得到相同的错误消息。这发生在我身上,因为我已将我的 Anaconda 安装移动到不同的目录。调试器会检查与 python 位于同一位置的 qt.conf 文件。运行 import sys; print sys.executable 可以找到该位置。我通过一堆网络搜索找到了这个解决方案,它被埋得很深here。 qt.conf 文件需要有正确的路径才能使调试器工作。

我的 qt.conf 文件在记事本中如下所示:

[Paths]
Prefix = E:/python/Anaconda3_py35/Library
Binaries = E:/python/Anaconda3_py35/Library/bin
Libraries = E:/python/Anaconda3_py35/Library/lib
Headers = E:/python/Anaconda3_py35/Library/include/qt

我曾使用 conda-pack 打包环境并将其传输到另一台运行相同操作系统的 PC。我没有注意到 qt.conf 文件夹指向之前安装环境的 PC 上的绝对路径。将这些替换为新 PC 上的绝对路径解决了该问题。
t
the Tin Man

只需添加一个系统变量:

QT_QPA_PLATFORM_PLUGIN_PATH

并将其值设置为

C:\Python34\Lib\site-packages\PyQt4\plugins\platforms

瞧。完毕


这也适用于 anaconda 环境中的 Centos 盒子。基本上我必须在运行胶水程序 export QT_QPA_PLATFORM_PLUGIN_PATH=/opt/anaconda/plugins/platform 之前设置它
我的 PyQt5 文件夹没有 plugins 文件夹。
t
the Tin Man

我找到了适合我的解决方案。此解决方案包含一个代码片段,可在您从 Pyside2 或 PyQt5 包中导入任何模块之前添加。有关详细信息,请参阅“Qt platform plugin "windows" #2”。

此代码片段来自链接:

import os
import PySide2

dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

from PySide2.QtWidgets import *
'''
Your code goes here
'''

此解决方案适用于 PyQt5 和 PySide2 模块。我不知道它是否相关,但我之前在系统中添加了 QT_PLUGIN_PATH 环境变量。

该解决方案使我能够在 IDLE 中测试 PySide2 脚本。

但是,当我尝试运行捆绑脚本 (exe) 时,我遇到了同样的错误。

通过一些浅层的调试,很明显插件文件夹本身丢失了。我通过在适当的位置添加插件文件夹解决了这个问题:

C:\Users\xxxx\.spyder-py3\My_QtProjects\Project 1\dist\MyQt_1\PySide2\

这里建议了两个不同的环境变量和一些解决方案......显然这在 Qt5 的版本之间有所不同?对我来说,这个解决方案有效(Qt5.5.1)
我当时正在玩 PySide2,我之前看过的答案指向 PyQt5.xx,幸运的是,它适用于我的 PySide2
t
the Tin Man

如果 Pycharm 控制台或调试器显示“无法找到或加载 Qt 平台插件窗口”,则 Python EXE 文件可能位于 PyCharm 解释器的不同位置。您可以在文件 -> 设置 -> 解释器中手动选择它。

设置工作目录:文件 -> 设置 -> 构建、执行、部署 -> 控制台 -> Python 控制台 -> 工作目录。将其设置为所有代码所在的父目录。打开控制面板 -> 系统设置 -> 高级系统设置 -> 环境变量 -> 新建。设置变量名 QT_PLUGIN_PATH ,变量目录:Users\\Appdata\Local\Continuum\Anaconda2\Library\plugins。重启 Pycharm。


您可以从命令行为此使用:setx QT_PLUGIN_PATH c:\users\%username%\anaconda3\Library\plugins。对于创建用于修复安装问题的批处理文件很有用。
t
the Tin Man

我通过以下方式解决了它:

添加路径:\Anaconda3\Lib\site-packages\PyQt5\Qt\bin 到 PATH。

设置环境变量:QT_PLUGIN_PATH 为 \Anaconda3\Lib\site-packages\PyQt5\Qt\plugins 或 \Anaconda3\Library\plugins。

另外,您可以尝试: pyqt = os.path.dirname(PyQt5.__file__) os.environ['QT_PLUGIN_PATH'] = os.path.join(pyqt, "Qt/plugins")


我喜欢以编程方式添加 QT_PLUGIN_PATH 的方法。在我的例子中,它是来自 PyQt5 模块的 Qt5/plugins。设置环境变量似乎会干扰其他也使用 Qt 的已安装程序。
j
jdaz

首先,使用命令:

conda remove pyqt qt qtpy

然后安装使用:

conda install pyqt qt qtpy

这对我有用。


非常感谢。对我来说,虽然简单地使用 pip install PyQt5 安装 PyQt5 解决了这个问题。
t
the Tin Man

复制文件夹

\Anaconda3\Library\plugins\platforms

\$\

其中 $ 是您的项目解释器文件夹。例如:

"\project\anaconda_env\Scripts\"

因为 PyCharm 调用此文件夹中的 python.exe,而不是 \Anaconda3 中的那个。


P
Pawel

WINDOWS 用户解决方案

使用以下命令创建新的环境变量:

名称:QT_PLUGIN_PATH 路径:C:\yourpythonpath\Lib\site-packages\PyQt5\Qt\plugins

之后那个exe文件就可以工作了


这已在此问题的另一个答案中提及
t
the Tin Man

在 Windows 上:

复制文件夹platforms:C:\Users\%USERNAME%\AppData\Roaming\pyinstaller\bincache00_py35_64bit\pyqt5\qt\plugins\platforms 将文件夹platforms粘贴到文件.exe的文件夹位置: 示例:c:\MyFolder\ yourFile.exe c:\MyFolder\platforms


t
the Tin Man

您可能需要复制 Anaconda3\Library 中的“插件”文件。例如,在我的电脑上是

S:\Anaconda3\Library\plugins

到您的 .exe 文件的相同路径。


y
yptheangel

从 PySide2 复制插件并粘贴并覆盖 Miniconda 中对我有用的现有插件。

(base) C:\ProgramData\Miniconda3\Lib\site-packages\PySide2\plugins\platforms>copy *.dll  C:\ProgramData\Miniconda3\Library\plugins\platforms\

使用 miniforge 包管理器在 Windows 计算机上使用 Pyside6(安装了 pip)有同样的问题。您的解决方案在这里也有效。谢谢
N
Nils

我对 Anaconda 也有同样的问题。对我来说,虽然不是很优雅,但最快的解决方案是完全卸载并重新安装 Ananconda。在那之后,一切都再次运行良好。


A
Abdelrahman Farag

我有同样的问题并以这种方式修复在 Anaconda 安装文件夹中我去了:(将其更改为您的安装路径): C:\ProgramData\Anaconda3\Lib\site-packages\PySide2 通过添加以下代码行编辑此文件:

# below the line 23 type.__signature__
    pyside_package_dir =  os.path.abspath(os.path.dirname(__file__))
    dirname = os.path.dirname(__file__)
    plugin_path = os.path.join(dirname, 'plugins', 'platforms')
    os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

保存此文件并重试,问题应该消失了:)


t
the Tin Man

我遇到过同样的问题。遵循“Managing environments”中的“激活环境”解决了该问题。

在命令行中:

conda activate myenv

myenv=base 用于我的设置。


A
Ammar Nourallah

请在脚本中试试这个

qt_path= os.path.dirname(PyQt5.__file__)
os.environ['QT_PLUGIN_PATH'] = os.path.join(qt_path, "Qt/plugins")

D
Dharman

我知道上面的每个人都提供了各种方法来解决 OP 的问题。我只是想补充一些建议。

通过添加 QT_PLUGIN_PATH = C:\Users{YOUR_USERNAME}\Anaconda3\Library\plugins 作为您的本地机器环境变量,它有助于解决上述 OP 的 PyCharm 问题。但是,这会破坏您机器中的其他系统,例如:Dropbox 报告缺少 QT、AMD 设置无法启动(这发生在我这边)等。

https://i.stack.imgur.com/ZVxgN.png

这种方法不仅允许您的 PyCharm 的 python.exe 能够搜索这些 DLL,而且不会破坏其他系统的 QT 查找路径。

谢谢


这不是一个适当的解决方案。在实际修复之前只是一个快速的解决方法
D
Duane

我安装了一个包含我不需要的 QT-gui 的包。

所以我刚刚从我的环境中删除了所有的 Qt 模块。

pip freeze | grep -i qt

PyQt5==5.15.4
PyQt5-Qt5==5.15.2
PyQt5-sip==12.9.0
QtPy==1.9.0

pip uninstall PyQt5
pip uninstall PyQt5-Qt5
pip uninstall PyQt5-sip
pip uninstall QtPy

问题解决了。


X
Xiangqina Zhao

受到 Osama Adly 的启发,我认为这类问题都是由 Windows 平台上 Qt DLL 的 Anaconda 配置引起的。只需尝试在 Anaconda 之外的空环境中安装 PyQt/PySide,例如独立的 Python 程序。您会发现有关平台的插件位于 site-package 目录本身中。为了比较:

\site-packages\PyQt6\Qt6\plugins\platforms
\site-packages\PySide6\plugins\platforms

但似乎 Anaconda 包含一些依赖于 PyQt5 或 Qt 的软件。 Anaconda 将平台目录从 PyQt5 移动到另一个文件夹,并且在使用 Anaconda 时,该文件夹可能包含在 PATH 变量中。

\Anaconda3\Library\plugins\platforms

这可能会导致不必要的问题。这些 DLL 在不同代的 Qt 中保留相同的名称。例如,当我在使用 Anaconda 创建的虚拟环境中尝试 PySide6 时,它对 DLL 的调用会错误地使用 Qt5 DLL,而不是其文件夹中的 DLL。


请不要发布重复的答案。如果您想编辑 Osama Adly's answer 以改进它,您可以通过点击答案下方的 edit 链接来实现。有关详细信息,请参阅 How to Answer
i
ironmann350

如果您使用安装了 matplotlib 的 anaconda/miniconda。您必须安装卸载 anaconda/miniconda 并在没有 matplotlib 的情况下使用 miniconda,解决方法是使用普通 python 而不是 anaconda。

这是一个已知问题enter link description here


O
Orsiris de Jong

就我而言,为了让 PyQt5 在 Windows 上运行,我遇到了多个组合问题,请参阅 DLL load failed when importing PyQt5


N
Nikolai Shevchenko

我对 Qt 5.9 示例 btscanner.exe 也有同样的问题。在我的情况下有效的是:

创建 btscanner.exe 所在的文件夹(我的为 c:\temp\BlueTouth)从命令提示符 windeployqt.exe 运行如下: c:\qt\qt5.9.0\msvc2015\bin\windeployqt c:\temp\BlueTouth /* windeplyqt 是标准的 Qt 工具,用于将您的应用程序与任何需要的库或额外文件打包并准备部署到其他机器上 */ 结果应该是这样的:

C:\temp\BlueTouth\btscanner.exe 32 bit, release executable
Adding Qt5Svg for qsvgicon.dll
Skipping plugin qtvirtualkeyboardplugin.dll due to disabled dependencies.
Direct dependencies: Qt5Bluetooth Qt5Core Qt5Gui Qt5Widgets
All dependencies   : Qt5Bluetooth Qt5Core Qt5Gui Qt5Widgets
To be deployed     : Qt5Bluetooth Qt5Core Qt5Gui Qt5Svg Qt5Widgets
Warning: Cannot find Visual Studio installation directory, VCINSTALLDIR is not set.
Updating Qt5Bluetooth.dll.
Updating Qt5Core.dll.
Updating Qt5Gui.dll.
Updating Qt5Svg.dll.
Updating Qt5Widgets.dll.
Updating libGLESV2.dll.
Updating libEGL.dll.
Updating D3Dcompiler_47.dll.
Updating opengl32sw.dll.
Patching Qt5Core.dll...
Creating directory C:/temp/BlueTouth/iconengines.
Updating qsvgicon.dll.
Creating directory C:/temp/BlueTouth/imageformats.
Updating qgif.dll.
Updating qicns.dll.
Updating qico.dll.
Updating qjpeg.dll.
Updating qsvg.dll.
Updating qtga.dll.
Updating qtiff.dll.
Updating qwbmp.dll.
Updating qwebp.dll.
Creating directory C:/temp/BlueTouth/platforms.
Updating qwindows.dll.
Creating C:\temp\BlueTouth\translations...
Creating qt_bg.qm...
Creating qt_ca.qm...
Creating qt_cs.qm...
Creating qt_da.qm...
Creating qt_de.qm...
Creating qt_en.qm...
Creating qt_es.qm...
Creating qt_fi.qm...
Creating qt_fr.qm...
Creating qt_gd.qm...
Creating qt_he.qm...
Creating qt_hu.qm...
Creating qt_it.qm...
Creating qt_ja.qm...
Creating qt_ko.qm...
Creating qt_lv.qm...
Creating qt_pl.qm...
Creating qt_ru.qm...
Creating qt_sk.qm...
Creating qt_uk.qm...

如果您查看 c:\temp\BlueTouth 文件夹,将看到文件夹 iconengines、imageformats、platforms、translations 和文件 D3Dcompiler_47.dll、libEGL.dll、libGLESV2.dll、opengl32sw.dll、Qt5Bluetooth.dll、Qt5Core.dll 、Qt5Gui.dll、Qt5Svg.dll、Qt5Widgets.dll。

这些是在这台机器或另一台机器上运行 btscanner.exe 所需的所有文件和文件夹。只需将整个文件夹复制到另一台机器上并运行该文件。


a
aawan

从 Anaconda3\Library\plugins 复制平台并将其放入 Anaconda3.对于 env 将平台放在特定的 env\ 文件夹中