Skip to main content
 首页 » 编程设计

python之尝试加载 matplotlib 时 iPython notebook 出错

2025年05月04日7lidabo

我正在尝试通过观看此视频来学习 Pandas :https://www.youtube.com/watch?v=5JnMutdy6Fw .我下载了笔记本 ( https://github.com/brandon-rhodes/pycon-pandas-tutorial/ ),并加载了 Exercises-1。第一 block

%matplotlib inline 
import pandas as pd 

给我这个错误:

--------------------------------------------------------------------------- 
ImportError                               Traceback (most recent call last) 
<ipython-input-10-5761ac74df30> in <module>() 
----> 1 get_ipython().magic(u'matplotlib inline') 
      2 import pandas as pd 
 
/Users/***/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s) 
   2305         magic_name, _, magic_arg_s = arg_s.partition(' ') 
   2306         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) 
-> 2307         return self.run_line_magic(magic_name, magic_arg_s) 
   2308  
   2309     #------------------------------------------------------------------------- 
 
/Users/***/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line) 
   2226                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals 
   2227             with self.builtin_trap: 
-> 2228                 result = fn(*args,**kwargs) 
   2229             return result 
   2230  
 
/Users/***/anaconda/lib/python2.7/site-packages/IPython/core/magics/pylab.pyc in matplotlib(self, line) 
 
/Users/***/anaconda/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k) 
    191     # but it's overkill for just that one bit of state. 
    192     def magic_deco(arg): 
--> 193         call = lambda f, *a, **k: f(*a, **k) 
    194  
    195         if callable(arg): 
 
/Users/***/anaconda/lib/python2.7/site-packages/IPython/core/magics/pylab.pyc in matplotlib(self, line) 
     86         """ 
     87         args = magic_arguments.parse_argstring(self.matplotlib, line) 
---> 88         gui, backend = self.shell.enable_matplotlib(args.gui) 
     89         self._show_matplotlib_backend(args.gui, backend) 
     90  
 
/Users/***/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in enable_matplotlib(self, gui) 
   3087         """ 
   3088         from IPython.core import pylabtools as pt 
-> 3089         gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) 
   3090  
   3091         if gui != 'inline': 
 
/Users/***/anaconda/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in find_gui_and_backend(gui, gui_select) 
    237     """ 
    238  
--> 239     import matplotlib 
    240  
    241     if gui and gui != 'auto': 
 
/Users/***/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/__init__.py in <module>() 
    178 # cbook must import matplotlib only within function 
    179 # definitions, so it is safe to import from it here. 
--> 180 from matplotlib.cbook import is_string_like 
    181 from matplotlib.compat import subprocess 
    182  
 
/Users/***/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/cbook.py in <module>() 
     31 from weakref import ref, WeakKeyDictionary 
     32  
---> 33 import numpy as np 
     34 import numpy.ma as ma 
     35  
 
/Users/***/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/__init__.py in <module>() 
    183         return loader(*packages, **options) 
    184  
--> 185     from . import add_newdocs 
    186     __all__ = ['add_newdocs', 
    187                'ModuleDeprecationWarning', 
 
/Users/***/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/add_newdocs.py in <module>() 
     11 from __future__ import division, absolute_import, print_function 
     12  
---> 13 from numpy.lib import add_newdoc 
     14  
     15 ############################################################################### 
 
/Users/***/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/lib/__init__.py in <module>() 
     15 from .ufunclike import * 
     16  
---> 17 from . import scimath as emath 
     18 from .polynomial import * 
     19 #import convertcode 
 
ImportError: cannot import name scimath 

谁能告诉我发生了什么事,我该如何解决?

请您参考如下方法:

您可以阅读类似的问题 here和 here .

主要问题在于导入 scimath: “导入错误:无法导入名称 scimath”

您可以尝试导入 scimath。

更好的解决方案是设置您的 PYTHONPATH var,它为 python 解释器提供额外的目录以查找 python 包/模块。您可以阅读有关设置 PYTHONPATH 变量的更多信息 here .