错误 qt.qpa.plugin: Could not find the Qt platform plugin “windows“ in ““ 的解决方法

系统环境:
  • Windows 10
  • Python 3.7.8
  • Pipenv 【Python虚拟环境,可选,如需了解可参考:https://blog.csdn.net/weixin_40922744/article/details/103723069】
  • PyQt5 5.15.2 【安装方式:pipenv install PyQt5
  • PySide2 5.15.2 【安装方式:pipenv install PySide2
    PyQt5 和PySide2 二选一安装即可
PySide2 和 PyQt5 的区别:

相同点:两者都是QT与Python结合的桥梁;
不同点:PyQt的开发者是英国的“Riverbank Computing”公司,而PySide则是由Qt的开发公司诺基亚发布的,两者的主要区别主要是提供的授权方式不同:PyQt遵守GPLv3协议,而PySide则是LGPL协议,前者可以免费地用于自由软件的开发,而后者属于闭源商用。更详细资料可参考:PySide2与PyQt5区别

错误描述:

在使用PySide2 或者 PyQt5 的过程中可能会出现以下错误:
错误 qt.qpa.plugin: Could not find the Qt platform plugin “windows“ in ““ 的解决方法_第1张图片
根据给出的提示可以看出错误原因是系统找不到Qt平台的插件,解决方法则是将PySide2或者PyQt5的plugins目录加入到QT_QPA_PLATFORM_PLUGIN_PATH环境变量里面。

解决方案:

上面部分已经说了解决思路,具体的实施方式有以下两种:

  1. 添加启动代码【简单重复】:

    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
    

    如果是PyQt5则需要修改启动代码为:

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

    在引入各类包的后面插入上述代码:
    错误 qt.qpa.plugin: Could not find the Qt platform plugin “windows“ in ““ 的解决方法_第2张图片

  2. 修改配置文件【一劳永逸】
    如果不想每次在代码前添加上述代码,可以修改PySide2的初始化文件"c:\users\XXX.virtualenvs\pyqt5-3bre18dc\lib\site-packages\PySide2_init_.py" ,在下图位置添加上述代码即可:
    错误 qt.qpa.plugin: Could not find the Qt platform plugin “windows“ in ““ 的解决方法_第3张图片
    对于PyQt5则需要手动添加PyQt的环境变量:
    错误 qt.qpa.plugin: Could not find the Qt platform plugin “windows“ in ““ 的解决方法_第4张图片

如需快速查找使用pip命令安装的包的位置可以使用pip show model_name:
错误 qt.qpa.plugin: Could not find the Qt platform plugin “windows“ in ““ 的解决方法_第5张图片

参考文献
  • pyside2出现qt.qpa.plugin: Could not find the Qt platform plugin “windows” in ""错误解决办法
  • https://github.com/pyqt/python-qt5/issues/2
  • https://doc.qt.io/qtforpython-5/quickstart.html#installation

你可能感兴趣的:(Python,Qt,python,qt5)