我们在使用PySide2对Python程序进行图形化时,出现弹窗报错:
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: direct2d, minimal, offscreen, windows.
如下图:
解决方法:添加环境变量
如果您使用Anaconda,首先找到PySide2库存放路径:
D:\Anaconda3\envs\pytorch\Lib\site-packages\PySide2
(每个人的安装路径不同,但后面的路径都一样)
然后在该文件夹下找到文件:
__init__.py
将该Python文件前面插入以下三行代码:
dirname = os.path.dirname(__file__)
plugin_path = os.path.join(dirname,'plugins','platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
使其变为:
from __future__ import print_function
import os
import sys
from textwrap import dedent
# 以下为插入的代码块
dirname = os.path.dirname(__file__)
plugin_path = os.path.join(dirname,'plugins','platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
# 以上为插入的代码块
__all__ = list("Qt" + body for body in
"Core;Gui;Widgets;PrintSupport;Sql;Network;Test;Concurrent;WinExtras;Xml;XmlPatterns;Help;Multimedia;MultimediaWidgets;OpenGL;OpenGLFunctions;Positioning;Location;Qml;Quick;QuickControls2;QuickWidgets;RemoteObjects;Scxml;Script;ScriptTools;Sensors;SerialPort;TextToSpeech;Charts;Svg;DataVisualization;UiTools;AxContainer;WebChannel;WebEngineCore;WebEngine;WebEngineWidgets;WebSockets;3DCore;3DRender;3DInput;3DLogic;3DAnimation;3DExtras"
.split(";"))
__version__ = "5.15.2.1"
__version_info__ = (5, 15, 2.1, "", "")
重新运行程序,没有报错,问题解决: