anaconda - python ImportError: 无法导入名称'QtCore'

我导入时收到以下错误。它与pandas导入有关,我不确定如何调试/解决这个问题。

导入:


import pandas as pd


import numpy as np


import pdb, math, pickle


import matplotlib.pyplot as plt



错误:


In [1]: %run NN.py


---------------------------------------------------------------------------


ImportError Traceback (most recent call last)


/home/abhishek/Desktop/submission/a1/new/NN.py in <module>()


 2 import numpy as np


 3 import pdb, math, pickle


----> 4 import matplotlib.pyplot as plt


 5 


 6 class NN(object):



/home/abhishek/anaconda3/lib/python3.5/site-packages/matplotlib/pyplot.py in <module>()


 112 


 113 from matplotlib.backends import pylab_setup


--> 114 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()


 115 


 116 _IP_REGISTERED = None



/home/abhishek/anaconda3/lib/python3.5/site-packages/matplotlib/backends/__init__.py in pylab_setup()


 30 # imports. 0 means only perform absolute imports.


 31 backend_mod = __import__(backend_name,


---> 32 globals(),locals(),[backend_name],0)


 33 


 34 # Things we pull in from all backends



/home/abhishek/anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_qt4agg.py in <module>()


 16 


 17 


---> 18 from .backend_qt5agg import FigureCanvasQTAggBase as _FigureCanvasQTAggBase


 19 


 20 from .backend_agg import FigureCanvasAgg



/home/abhishek/anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py in <module>()


 14 


 15 from .backend_agg import FigureCanvasAgg


---> 16 from .backend_qt5 import QtCore


 17 from .backend_qt5 import QtGui


 18 from .backend_qt5 import FigureManagerQT



/home/abhishek/anaconda3/lib/python3.5/site-packages/matplotlib/backends/backend_qt5.py in <module>()


 29 figureoptions = None


 30 


---> 31 from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, __version__


 32 from matplotlib.backends.qt_editor.formsubplottool import UiSubplotTool


 33 



/home/abhishek/anaconda3/lib/python3.5/site-packages/matplotlib/backends/qt_compat.py in <module>()


 135 # have been changed in the above if block


 136 if QT_API in [QT_API_PYQT, QT_API_PYQTv2]: # PyQt4 API


--> 137 from PyQt4 import QtCore, QtGui


 138 


 139 try:



ImportError: cannot import name 'QtCore'



调试:


$ python -c"import PyQt4"


$ python -c"from PyQt4 import QtCore"


Traceback (most recent call last):


 File"<string>", line 1, in <module>


ImportError: cannot import name 'QtCore'


$ conda list | grep qt


jupyter-qtconsole-colorschemes 0.7.1 <pip>


pyqt 5.6.0 py35_0 


qt 5.6.0 0 


qtawesome 0.3.3 py35_0 


qtconsole 4.2.1 py35_0 


qtpy 1.0.2 py35_0 



时间:

将pyqt版本5.6.0降级到4.11.4,并将qt从版本5.6.0降级到4.8.7可以解决此问题:


$ conda install pyqt=4.11.4


$ conda install qt=4.8.7



https://github.com/ContinuumIO/anaconda-issues/issues/1068

为了避免降级,你还可以将后端设置为使用Qt5Agg,方法是:http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

你可以修改matplotlibrc文件/your/path/to/anaconda3/envs/yourenv/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc,并将后端更改为Qt5Agg ,这将使Qt5Agg后端在yourenv conda环境中成为默认设置。

更新matplotlib对我有帮助:


conda install matplotlib



只有这对我有效:


sudo pip install matplotlib --upgrade



我找到了解决这个问题的方法,我认为最好不要降级包,

import matplotlib matplotlib.use('Qt5Agg')

文档

我找到了一个简单的修复方法。

查看"qt_compat.py"文件,问题是PyQt5不在sys.modules中,我尝试先导入它,这就解决了问题。


import PyQt5


import matplotlib.pyplot as plt



...