亲测有效,一招解决错误:This application failed to start because not Qt platform plugin could be initialized.

运行Pyside2时出现如下图所示错误:
错误内容:This application failed to start because not Qt platform plugin could be initialized.Reinstalling the application may fix this problem.
亲测有效,一招解决错误:This application failed to start because not Qt platform plugin could be initialized._第1张图片

解决方案一:此方法一劳永逸。

将PySide2路径下的plugins\platforms添加到系统环境变量中用户变量里。我的路径如下:D:\soft\Anaconda\Lib\site-packages\PySide2\plugins\platforms
具体操作如下:
亲测有效,一招解决错误:This application failed to start because not Qt platform plugin could be initialized._第2张图片
亲测有效,一招解决错误:This application failed to start because not Qt platform plugin could be initialized._第3张图片
变量名:QT_QPA_PLATFORM_PLUGIN_PATH
变量值:D:\soft\Anaconda\Lib\site-packages\PySide2\plugins\platforms
不同用户,变量名一致,变量值随文件位置变。
最后,保存变量后,一定要重启电脑,因为新的环境变量要重启后才能被系统识别!

**

解决方案二:

**
在程序初始部分添加如下代码:

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

代码意思是用临时系统环境变量,功能与解决方案一类似。

你可能感兴趣的:(pyside2,pyqt,UI,python)