ImportError: cannot import name 'QtCore'

2019-03-14 18:25发布

问题:

I am getting the below error with the following imports. It seems to be related to pandas import. I am unsure how to debug/solve this.

Imports:

import pandas as pd
import numpy as np
import pdb, math, pickle
import matplotlib.pyplot as plt

Error:

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'

Debugging:

$ 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 

I found other answers but all related to Windows. I am using ubuntu 16.04 with anaconda distribution of python 3.

回答1:

Downgrading pyqt version 5.6.0 to 4.11.4, and qt from version 5.6.0 to 4.8.7 fixes this:

$ conda install pyqt=4.11.4
$ conda install qt=4.8.7

The issue itself is being resolved here: https://github.com/ContinuumIO/anaconda-issues/issues/1068



回答2:

To avoid downgrading you can also (as was suggested by tacaswell in the issue) set the backend to use Qt5Agg using one of the methods here: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

E.g., you can modify the matplotlibrc file located in /your/path/to/anaconda3/envs/yourenv/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc and change the backend line to backend : Qt5Agg. This will make the Qt5Agg backend the default throughout the yourenv conda environment.



回答3:

Updating matplotlib did the trick for me:

conda install matplotlib


回答4:

Allan Zelener's solution works for me. As a supplement,

  1. the "matplotlibrc" file location is determined in the following order

    • $PWD/matplotlibrc(current directory)

    • $MATPLOTLIBRC/matplotlibrc

    • $MPLCONFIGDIR/matplotlibrc

    • On Linux,

      • $HOME/.matplotlib/matplotlibrc, if it exists

      • or $XDG_CONFIG_HOME/matplotlib/matplotlibrc (if $XDG_CONFIG_HOME is defined)

      • or $HOME/.config/matplotlib/matplotlibrc (if $XDG_CONFIG_HOME is not defined)

    • On other platforms,

      • $HOME/.matplotlib/matplotlibrc if $HOME is defined.
    • Lastly, it looks in $MATPLOTLIBDATA/matplotlibrc for a system-defined copy.

  2. If you upgraded pyqt5 from pyqt4, pyqt4 could still exist in your Anoconda, though it was absent in "conda list". Even if you changed the backend from Qt4Agg to Qt5Agg, matplotlib still tended to use Qt4Agg according to Anaconda3\Lib\site-packages\matplotlib\backends\qt_compat.py.

    Therefore, if you installed pyqt 5.x for Anaconda but found both of PyQt4 and PyQt5 in Anaconda3\Lib\site-packages, you should delete the folder PyQt4 and leave PyQt5 alone.



回答5:

I tried all the above but failed.

Only this works for me:

sudo pip install matplotlib --upgrade


回答6:

I found a way to solve this problem, I think is better not to downgrade the package

import matplotlib
matplotlib.use('Qt5Agg')

documentation



回答7:

The suggested answers that avoid downgrading packages didn't end up working for me. However, I found a simple fix that did work for me.

Looking through the "qt_compat.py" file, the problem seems to be that PyQt5 is not in sys.modules, while it should be. I tried importing it first, which worked and fixed the problem.

import PyQt5
import matplotlib.pyplot as plt


回答8:

This issue showed up after I have tried to install ROS-gazebo simulator for PX4. Well, I just failed to make it work after following all the solutions. Fortunately, I was able to make it by deleting the PyQt5 package under /usr/lib/python2.7/dist-packages.... I am using conda environment, so maybe some scripts I ran before install a broken package using sudo command... Have no idea what's the magic