问题描述:

在python程序中调用了某个需要pyqt的包,运行中出现报错: qt.qpa.plugin: Could not find the Qt platform plugin “windows” in “” This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. 机器是windows11系统,已经安装过pyqt5。

失败尝试:

在windows power shell中运行pip show pyqt5,得到pytq5的安装路径为: C:\Users…\LocalCache\local-packages\Python311\site-packages (部分路径省略) 则相应的Qt的环境路径为: C:\Users…\LocalCache\local-packages\Python311\site-packages\PyQt5\Qt5\plugins\platforms 曾尝试在windows11的环境变量中加入名为“QT_QPA_PLATFORM_PLUGIN_PATH”的环境变量,变量内容为以上Qt的环境路径(在Win11中间添加环境变量的具体方法可以很容易在网上搜到。由于这个方法最终无效,所以就不在这里详细说明了。): 结果添加环境变量之后问题并没有解决。

成功尝试:

在python代码中加入以下代码:

import os, PyQt5

dirname = os.path.dirname(PyQt5.__file__)

qt_dir = os.path.join(dirname, 'Qt5', 'plugins', 'platforms')

os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = qt_dir

问题解决。

推荐文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: