英文:
How to resolve "no Qt Platform Plugin could be initialized" for a Qt5 application?
问题
我正在开发一个基于 PyQt5 的 Python 应用程序。一切都很顺利,直到我重装了 Windows,因为遇到了一些问题。我已经复制了我的环境,重新安装了 Anaconda,然后再次将环境复制到 env 文件夹中。现在的问题是,当我在 PyCharm IDE 中运行我的代码时,它显示了一个错误对话框,内容如下:
我尝试过多种解决方案,比如:
解决方案1:更改 QT 文件位置
有时,像更改 QT 文件位置这样的简单技巧就足以摆脱错误。以下是操作步骤:
- 打开文件资源管理器,进入此电脑。
- 使用搜索栏,搜索 pyqt5_tools。
- 当 Windows 完成搜索后,右键单击 pyqt5_tools,然后选择打开文件夹位置。
- 进入 PyQt5 > Qt > bin。复制 platforms 文件夹。<kbd>在我的情况下,此文件夹中没有 bin 文件夹</kbd>。
- 搜索 site-packages,然后打开该文件夹。
- 在那里,粘贴 platforms 文件夹。
- Windows 会警告你已经有一个同名文件夹。点击替换目标文件中的文件。
解决方案2:运行 SFC 扫描
有可能是因为系统文件损坏,导致 Windows 显示“应用程序无法初始化 QT 平台插件”错误。幸运的是,Windows 内置了一个工具来帮助你解决这个问题。
在开始菜单的搜索栏中,搜索命令提示符,然后选择以管理员身份运行。然后,运行 sfc /scannow 命令。Windows 会扫描并自动替换任何损坏的系统文件。
对我来说都没用,我一直得到相同的错误。我该如何解决上图中显示的错误? 任何帮助将不胜感激,谢谢。
英文:
I am working on a python application based on PyQt5. Everything was running good until I redo my PC and reinstall windows again because of some issue. I had copied my environment and after reinstalling Anaconda, I copied that environment again in env folder. Now the problem is that, when I run my code in PyCharm IDE, it displays and error dialog like this:
I have tried multiple solutions such as:
Solution 1: Change the QT Files Location
Sometimes, a simple trick such as changing the QT files location is enough to get rid of the error. Here’s how you can do it:
- Launch File Explorer and open This PC.
- Using the Search field, search for pyqt5_tools.
- When Windows finishes the search, right-click the pyqt5_tools and head to Open folder location.
- Head to PyQt5 > Qt > bin. Copy the platforms folder. <kbd>in my case bin is not available in this folder</kbd>
- Make a new search for site-packages and open the folder.
- There, paste the platforms folder.
- Windows will warn you there’s already a folder with the same name. Click Replace the files in the destination.
Solution 2: Run an SFC Scan
There’s a chance Windows display the “Application failed because no QT platform plugin could be initialized” error due to corrupt system files. Fortunately, Windows has a built-in tool to help you fix the problem.
In the Start menu search bar, search for command prompt and select Run as administrator. Then, run the sfc /scannow command line. Windows will scan and automatically replace any corrupted system file.
Nothing worked for me, I'm getting the same error all the time.
How can I resolve the error displayed in the figure above?
Any help will be appreciated, Thanks.
答案1
得分: 0
对这个问题的更新答案
-----------------------------------
[答案参考][1]
[1]: https://stackoverflow.com/questions/70896967/pycharm-has-no-qt-platform-plugin-could-be-initialized-error-when-using-matplotl
要将 PyQt5 的库路径添加到 PATH 环境变量中:
**1.** 打开 **`编辑环境变量`** 对话框。
**2.** 选择适当的 **`Path`** 变量(可以是 **用户** 或 **系统** 变量)。
(注意:我在 **系统变量** 中做了更改)
**3.** 编辑变量并将 **`PyQt5`** 库路径添加为三个条目放在顶部。
**4.** **添加以下 3 个条目**,确保条目在 **所需顺序** 并且在 **路径变量的顶部**。
**5.** 保存更改。
```plaintext
C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\bin
C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\plugins
C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\plugins\platforms
6. 此外,如果你有一个名为:QT_PLUGIN_PATH
的变量,你希望将此行放在列表的 顶部:
C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\plugins
(注意:在我的情况下,Anaconda 是为所有用户安装的,因此,Anaconda 将位于 C:\ProgramData
)
7. 一旦你保存了对环境变量的更改,需要重新启动 PyCharm,以便它能够识别并找到更新的库。
<details>
<summary>英文:</summary>
Updated Answer to this question
-----------------------------------
[Answer Reference][1]
[1]: https://stackoverflow.com/questions/70896967/pycharm-has-no-qt-platform-plugin-could-be-initialized-error-when-using-matplotl
To add PyQt5's library path to your PATH environment variable:
**1.** Open the **`Edit Environment Variables`** dialog.
**2.** Select the appropriate **`Path`** variable (either **User** or **System** variables).
(Note: I have changed in **`system variables`**)
**3.** Edit the variable and add the **`PyQt5`** library path as three entries at the top.
**4.** **Add the following 3 entries**, and make sure the entries are in the **desired order** and at the **top of path** variable.
**5.** Save the changes.
C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\bin
C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\plugins
C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\plugins\platforms
**6.** Also, if you have a variable named: **`QT_PLUGIN_PATH`**, you want to put this line at the **top of the list**:
C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\plugins
(Note: In my case, Anaconda is installed for all users so, Anaconda will be found in **`C:\ProgramData`**)
**7.** Once you have saved the changes to your environment variables, it is necessary to **Restart PyCharm** in order for it to recognize and locate the updated libraries..
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论