在python中用matplotlab画图,显示
RuntimeError: The environment variable QT_API has the unrecognized value '5.12.0'; valid values are pyqt6, pyside6, pyqt5, pyside2
在错误信息的最后一条
File "D:develop_toolspythonlibsite-packagesmatplotlibackendsqt_compat.py", line 67, in <module>
打开错误信息提到的qt_compat.py
if sys.modules.get("PyQt6.QtCore"):
QT_API = QT_API_PYQT6
elif sys.modules.get("PySide6.QtCore"):
QT_API = QT_API_PYSIDE6
elif sys.modules.get("PyQt5.QtCore"):
QT_API = QT_API_PYQT5
elif sys.modules.get("PySide2.QtCore"):
QT_API = QT_API_PYSIDE2
# Otherwise, check the QT_API environment variable (from Enthought). This can
# only override the binding, not the backend (in other words, we check that the
# requested backend actually matches). Use _get_backend_or_none to avoid
# triggering backend resolution (which can result in a partially but
# incompletely imported backend_qt5).
elif (mpl.rcParams._get_backend_or_none() or "").lower().startswith("qt5"):
if QT_API_ENV in ["pyqt5", "pyside2"]:
QT_API = _ETS[QT_API_ENV]
else:
_QT_FORCE_QT5_BINDING = True # noqa
QT_API = None
# A non-Qt backend was selected but we still got there (possible, e.g., when
# fully manually embedding Matplotlib in a Qt app without using pyplot).
elif QT_API_ENV is None:
QT_API = None
elif QT_API_ENV in _ETS:
QT_API = _ETS[QT_API_ENV]
else:
raise RuntimeError( #67行
"The environment variable QT_API has the unrecognized value {!r}; "
"valid values are {}".format(QT_API_ENV, ", ".join(_ETS)))
本机装了PyQt5,上方代码中
elif sys.modules.get("PyQt5.QtCore"):
QT_API = QT_API_PYQT5
应该没有执行
查看 sys.modules 的功能
python中的sys.modules是一个全局字典,从python程序启动就加载到了内存,用于保存当前已导入(加载)的所有模块名和模块对象
应该是PyQt5.QtCore没有导入,导致没有执行这个elif
不知怎样,直接在if上方添加导入的import PyQt5.QtCore
import PyQt5.QtCore #加入
if sys.modules.get("PyQt6.QtCore"):
QT_API = QT_API_PYQT6
elif sys.modules.get("PySide6.QtCore"):
QT_API = QT_API_PYSIDE6
elif sys.modules.get("PyQt5.QtCore"):
QT_API = QT_API_PYQT5
保存文件。
重新画图,暂时运行正常。