无法使用最新的conda导入matplotlib.pyplot

时间:2016-10-13 18:55:25

标签: python matplotlib pyqt conda

我使用Python 2.7.12运行Conda版本4.2.9,我已经用新的conda环境确认了这个错误,只有matplotlib包

当我尝试导入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

我已经广泛搜索了这个问题并看到有一些临时解决方案,但是,我不知道如何使用其中一个而另一个不适合我。

一种解决方案是手动设置matplotlib的后端,如下所示:

>>> 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

你可以看到这对我失败了。另一个解决方案是使用补丁(sourced from this thread)。不幸的是,我不知道如何使用这个补丁或如何将#py;引脚传输到4.11&#34;。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

固定包的解释为here

The patch非常小,您可以手动申请。查找您的matplotlibrc文件(可能是.config/matplotlib/matplotlibrc),并通过编辑以下行将默认后端设置为Qt5Agg:

backend      : Qt5Agg
backend.qt5  : PyQt5

但是,由于你的程序中已经有use("Qt5Agg"),它会覆盖rcfile,我认为这不会有帮助。

我认为您的问题是由QT_API环境变量设置为PyQt4(或PySide)引起的。例如,通过向您的程序添加import os; print(os.environ.get('QT_API'))来检查此项。如果是这种情况,请将行export QT_API=PyQt5添加到.bashrc(假设您运行Linux)。