Anaconda:无法导入pylab。

8

最新版本的Anaconda(Linux 64位)中,我无法使用import pylab。其他软件包似乎可以正常工作。(注意:我没有sudo访问权限)

In [1]: import pylab as pl
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-2cf12a0af6ff> in <module>()
----> 1 import pylab as pl

/home/josh/installs/conda/1.7.0/lib/python2.7/site-packages/pylab.py in <module>()
----> 1 from matplotlib.pylab import *
      2 import matplotlib.pylab
      3 __doc__ = matplotlib.pylab.__doc__

/home/josh/installs/conda/1.7.0/lib/python2.7/site-packages/matplotlib/pylab.py in <module>()
    267 from numpy.linalg import *
    268 
--> 269 from matplotlib.pyplot import *
    270 
    271 # provide the recommended module abbrevs in the pylab namespace

/home/josh/installs/conda/1.7.0/lib/python2.7/site-packages/matplotlib/pyplot.py in <module>()
     96 
     97 from matplotlib.backends import pylab_setup
---> 98 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
     99 
    100 

/home/josh/installs/conda/1.7.0/lib/python2.7/site-packages/matplotlib/backends/__init__.pyc in pylab_setup()
     23         backend_name = 'matplotlib.backends.%s'%backend_name.lower()
     24     backend_mod = __import__(backend_name,
---> 25                              globals(),locals(),[backend_name])
     26 
     27     # Things we pull in from all backends

/home/josh/installs/conda/1.7.0/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py in <module>()
     11 
     12 from backend_agg import FigureCanvasAgg
---> 13 from backend_qt4 import QtCore, QtGui, FigureManagerQT, FigureCanvasQT,\
     14      show, draw_if_interactive, backend_version, \
     15      NavigationToolbar2QT

/home/josh/installs/conda/1.7.0/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py in <module>()
     23     figureoptions = None
     24 
---> 25 from qt4_compat import QtCore, QtGui, _getSaveFileName, __version__
     26 
     27 backend_version = __version__

/home/josh/installs/conda/1.7.0/lib/python2.7/site-packages/matplotlib/backends/qt4_compat.py in <module>()
     34 # Now perform the imports.
     35 if QT_API in (QT_API_PYQT, QT_API_PYQTv2):
---> 36     import sip
     37     if QT_API == QT_API_PYQTv2:
     38         if QT_API_ENV == 'pyqt':

ImportError: No module named sip

如果我尝试

> conda install sip 

I get:

Error: No packages found matching: sip

我也遇到了以下错误:

$ pip install sip                                                       1 
Downloading/unpacking sip
  You are installing a potentially insecure and unverifiable file. Future v
ersions of pip will default to disallowing insecure files.                
  Downloading sip-4.15.2.zip (899kB): 899kB downloaded
  Running setup.py egg_info for package sip
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
    IOError: [Errno 2] No such file or directory: '/tmp/pip_build_josh/
sip/setup.py'                                                             
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

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

IOError: [Errno 2] No such file or directory: '/tmp/pip_build_josh/sip/
setup.py'                                                                 

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build
_josh/sip                                                             
Storing complete log in /home/josh/.pip/pip.log

为什么会这样?您有什么想法吗?

您似乎没有正确安装PyQt,请使用其他后端。 - tacaswell
谢谢@tcaswell。你还推荐其他后端吗? - Josh
1
讽刺的(但大多数是正确的)答案是使用你已经安装了依赖项的任何一个。尝试使用 tkaggwxagggtk3agg - tacaswell
请访问http://matplotlib.org/faq/usage_faq.html#what-is-a-backend,向下滚动获取完整列表。 - tacaswell
2个回答

10

我刚遇到了这个问题,与matplotlib尝试使用的qt后端有关,请尝试以下操作:

import PyQt4

如果您没有PyQt4,您可能有PySide

import PySide
如果这是情况,您需要设置matplotlib.rcParams['backend.qt4'] == 'PySide'而不是'PyQt4'。您还可以在matplotlibrc文件(〜/ .matplotlib / matplotlibrc)中执行此操作。
# find and change line:
backend.qt4 : PySide
注意:如果您没有matplotlibrc文件,可以从matplotlib源目录中复制它。
import os
import matplotlib
mpl_dir = os.path.dirname(matplotlib.__file__)
os.system("cp {}/mpl-data/matplotlibrc ~/.matplotlib/".format(mpl_dir))

Anaconda针对此问题存在一个未解决的问题:https://github.com/ContinuumIO/anaconda-issues/issues/8 - user2304916

0

以下方法对我有效

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pylab as pl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接