我使用anaconda搭建的环境,自带了spyder,觉得和matlab界面很像,比较容易上手。开始并没有什么问题,但是不知道什么时候出现了如下问题
NOTE: The following error appeared when setting your Matplotlib backend
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\ipython\spyder_kernel.py", line 498, in _set_mpl_backend
get_ipython().run_line_magic(magic, backend)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2095, in run_line_magic
result = fn(*args,**kwargs)
File "", line 2, in matplotlib
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\magic.py", line 187, in
call = lambda f, *a, **k: f(*a, **k)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\magics\pylab.py", line 99, in matplotlib
gui, backend = self.shell.enable_matplotlib(args.gui)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2978, in enable_matplotlib
pt.activate_matplotlib(backend)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\pylabtools.py", line 307, in activate_matplotlib
import matplotlib.pyplot
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 115, in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\__init__.py", line 62, in pylab_setup
[backend_name], 0)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt4agg.py", line 9, in
from .backend_qt5agg import (
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 15, in
from .backend_qt5 import (
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt5.py", line 19, in
import matplotlib.backends.qt_editor.figureoptions as figureoptions
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\qt_editor\figureoptions.py", line 20, in
import matplotlib.backends.qt_editor.formlayout as formlayout
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\qt_editor\formlayout.py", line 54, in
from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\qt_compat.py", line 153, in
from PyQt4 import QtCore, QtGui
ModuleNotFoundError: No module named 'PyQt4'
虽然大部分时候并不影响使用,但用matplotlib包的时候问题就出现了,而且启动就有真么一串的错误提示,简直不能忍啊!于是每天晚上都要花时间来处理这个错误,可是查了好多天也没找到一个有效的解决办法,唯一的一个有用的线索就是说anaconda和PyQt4存在冲突问题,更新就好了,然并卵。最终还是得靠寄几个儿啊。来吧,撸起袖子do!
仔细看出错的地方是因为导入PyQt4的时候有问题,那么就看一下是否安装了,pip list一下,果然没有安装,可是安装了PyQt5啊,难不成是因为PyQt5不被识别?卸载了,无效,更新anaconda,更新不了,不知道为啥。那好吧,我安装PyQt4总可以吧,你妹的pip找不到资源,终于找到了whl文件,居然安装也出问题!!!我还想写论文呢,时间都浪费在你这了。
硬着头皮,打开出错的文件:qtcompat.py,好在不是特别复杂,debug了一下,发先是因为matplotlib默认使用的是pyqt4(这里不太理解,只是有一个rcParams['backend']的参数是pyqt4),qtcompat文件的逻辑是对系统中存在的一堆版本进行判断以后,最终要调用rcParams标记的版本==!,那前面的判断感觉没啥卵用啊,好吧,那只好在导入matplotlib后强制将rcParams['backend']设置为PyQt5了。然后错误果然没有了,但是仍然有些问题
NOTE: Spyder *can't* set your selected Matplotlib backend because there is a previous backend already in use.
Your backend will be Qt4Agg
不过看着舒服多了,而且matplotlib也可以用了
然后进一步想试一下会不会是matplotlib的问题,更新这个模块是否可以呢。更新后发现qtcompat文件变了,错误重新出现。那么肯定是matplotlib的问题了,只能这样凑合用了。