简体   繁体   English

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

[英]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 我正在使用Python 2.7.12运行Conda版本4.2.9,我已经在新的conda环境和仅matplotlib软件包中确认了此错误。

My problem occurs when I try to import matplotlib.pyplot: 当我尝试导入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: 一种解决方案是手动设置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

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". 不幸的是,我不知道如何使用此补丁程序或如何将pyqt固定到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: 查找您的matplotlibrc文件(可能是.config/matplotlib/matplotlibrc ),然后通过编辑以下几行将默认后端设置为Qt5Agg:

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. 但是,由于您的程序中已经有use("Qt5Agg") (它覆盖了rcfile),所以我认为这不会有所帮助。

I think your issue is caused by having a QT_API environment variable that still is set to PyQt4 (or PySide ). 我认为您的问题是由于QT_API环境变量仍然设置为PyQt4 (或PySide )引起的。 Check this, for instance, by adding import os; print(os.environ.get('QT_API')) 例如,通过添加import os; print(os.environ.get('QT_API')) import os; print(os.environ.get('QT_API')) to your program. import os; print(os.environ.get('QT_API'))到您的程序。 If this is the case, add the line export QT_API=PyQt5 to your .bashrc (assuming you run Linux). 在这种情况下,请将export QT_API=PyQt5行添加到您的.bashrc (假设您运行Linux)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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