简体   繁体   中英

Unable to import matplotlib.pyplot with latest conda

I am running Conda version 4.2.9 with Python 2.7.12, I have confirmed this bug with a fresh conda environment and only the matplotlib package

My problem occurs when I try to import matplotlib.pyplot:

>>> import matplotlib.pyplot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/pyplot.py", line 114, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 16, in <module>
    from .backend_qt5 import QtCore
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 31, in <module>
    from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, __version__
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/backends/qt_compat.py", line 137, in <module>
    from PyQt4 import QtCore, QtGui
ImportError: No module named PyQt4

I have googled this issue extensively and see that there are some temporary solutions, however, I don't know how to use one of them and the other one doesn't work for me.

One solution was to set the backend for matplotlib manually like so:

>>> import matplotlib
>>> matplotlib.use('Qt5Agg')
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/pyplot.py", line 114, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 16, in <module>
    from .backend_qt5 import QtCore
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 31, in <module>
    from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, __version__
  File "/home/me/anaconda2/envs/snowflake/lib/python2.7/site-packages/matplotlib/backends/qt_compat.py", line 137, in <module>
    from PyQt4 import QtCore, QtGui
ImportError: No module named PyQt4

As you can see this failed for me. The other solution was to use a patch ( sourced from this thread ). Unfortunately I do not know how to use this patch or how to "pin pyqt to 4.11". Can anyone help?

Pinning packages is explained here .

The patch is so small you can apply it by hand. Look for your matplotlibrc file (probably .config/matplotlib/matplotlibrc ), and set the default backend to Qt5Agg by editing the following lines:

backend      : Qt5Agg
backend.qt5  : PyQt5

However, since you already have use("Qt5Agg") in your program, which overrides the rcfile, I don't think this will help.

I think your issue is caused by having a QT_API environment variable that still is set to PyQt4 (or PySide ). Check this, for instance, by adding import os; print(os.environ.get('QT_API')) import os; print(os.environ.get('QT_API')) to your program. If this is the case, add the line export QT_API=PyQt5 to your .bashrc (assuming you run Linux).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM