1

我有一个带有以下代码的简单笔记本:

%matplotlib inline

但是,在运行它时,我收到以下错误:

ImportError: No module named _tkinter

我在同一个项目中有另一个笔记本,它能够毫无问题地运行该语句。

数据科学体验是一项托管服务,因此您没有安装 _tkinter 的 root 访问权限。

完整的堆栈跟踪:

ImportErrorTraceback (most recent call last)
<ipython-input-43-5f9c00ae8c2d> in <module>()
----> 1 get_ipython().magic(u'matplotlib inline')
      2 
      3 import matplotlib.pyplot as plt
      4 #import numpy as np
      5 

/usr/local/src/bluemix_jupyter_bundle.v20/notebook/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2161         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2162         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2163         return self.run_line_magic(magic_name, magic_arg_s)
   2164 
   2165     #-------------------------------------------------------------------------

/usr/local/src/bluemix_jupyter_bundle.v20/notebook/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2082                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2083             with self.builtin_trap:
-> 2084                 result = fn(*args,**kwargs)
   2085             return result
   2086 

<decorator-gen-106> in matplotlib(self, line)

/usr/local/src/bluemix_jupyter_bundle.v20/notebook/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):

/usr/local/src/bluemix_jupyter_bundle.v20/notebook/lib/python2.7/site-packages/IPython/core/magics/pylab.pyc in matplotlib(self, line)
     98             print("Available matplotlib backends: %s" % backends_list)
     99         else:
--> 100             gui, backend = self.shell.enable_matplotlib(args.gui)
    101             self._show_matplotlib_backend(args.gui, backend)
    102 

/usr/local/src/bluemix_jupyter_bundle.v20/notebook/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in enable_matplotlib(self, gui)
   2949                 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
   2950 
-> 2951         pt.activate_matplotlib(backend)
   2952         pt.configure_inline_support(self, backend)
   2953 

/usr/local/src/bluemix_jupyter_bundle.v20/notebook/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in activate_matplotlib(backend)
    293     matplotlib.rcParams['backend'] = backend
    294 
--> 295     import matplotlib.pyplot
    296     matplotlib.pyplot.switch_backend(backend)
    297 

/gpfs/fs01/user/sdd1-7e9fd7607be53e-39ca506ba762/.local/lib/python2.7/site-packages/matplotlib/pyplot.py in <module>()
    112 
    113 from matplotlib.backends import pylab_setup
--> 114 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
    115 
    116 _IP_REGISTERED = None

/gpfs/fs01/user/sdd1-7e9fd7607be53e-39ca506ba762/.local/lib/python2.7/site-packages/matplotlib/backends/__init__.pyc in pylab_setup()
     30     # imports. 0 means only perform absolute imports.
     31     backend_mod = __import__(backend_name,
---> 32                              globals(),locals(),[backend_name],0)
     33 
     34     # Things we pull in from all backends

/gpfs/fs01/user/sdd1-7e9fd7607be53e-39ca506ba762/.local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py in <module>()
      4 
      5 from matplotlib.externals import six
----> 6 from matplotlib.externals.six.moves import tkinter as Tk
      7 from matplotlib.externals.six.moves import tkinter_filedialog as FileDialog
      8 

/gpfs/fs01/user/sdd1-7e9fd7607be53e-39ca506ba762/.local/lib/python2.7/site-packages/matplotlib/externals/six.pyc in load_module(self, fullname)
    197         mod = self.__get_module(fullname)
    198         if isinstance(mod, MovedModule):
--> 199             mod = mod._resolve()
    200         else:
    201             mod.__loader__ = self

/gpfs/fs01/user/sdd1-7e9fd7607be53e-39ca506ba762/.local/lib/python2.7/site-packages/matplotlib/externals/six.pyc in _resolve(self)
    111 
    112     def _resolve(self):
--> 113         return _import_module(self.mod)
    114 
    115     def __getattr__(self, attr):

/gpfs/fs01/user/sdd1-7e9fd7607be53e-39ca506ba762/.local/lib/python2.7/site-packages/matplotlib/externals/six.pyc in _import_module(name)
     78 def _import_module(name):
     79     """Import module, returning the module after the last dot."""
---> 80     __import__(name)
     81     return sys.modules[name]
     82 

/usr/local/src/bluemix_jupyter_bundle.v20/notebook/lib/python2.7/lib-tk/Tkinter.py in <module>()
     37     # Attempt to configure Tcl/Tk without requiring PATH
     38     import FixTk
---> 39 import _tkinter # If this fails your Python may not be configured for Tk
     40 tkinter = _tkinter # b/w compat for export
     41 TclError = _tkinter.TclError

ImportError: No module named _tkinter
4

1 回答 1

2

所以修复非常简单——我只需要使用笔记本中的内核菜单项重新启动内核。

我在笔记本电脑上本地运行 ipython 时遇到了同样的问题,解决方案是安装 tkinter,所以我没想到答案会像重启内核一样简单。

另一次我收到此错误消息时,重新启动内核不起作用。我不得不:

  • 更改火花后端
  • 下载笔记本到文件
  • 删除 DSX 中的笔记本
  • 从下载的笔记本创建一个新笔记本
于 2016-10-12T13:05:31.253 回答