1

我有一個簡單的筆記本用下面的代碼:matplotlib - 導入錯誤:沒有模塊名爲_tkinter

%matplotlib inline 

但是,在運行時,我收到以下錯誤:

ImportError: No module named _tkinter 

我還有一個筆記本在同一個項目中,並且能夠毫無問題地運行該聲明。

數據科學體驗是一種託管服務,因此您沒有root訪問權限來安裝_tkinter。

完整堆棧跟蹤:

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

一個新的筆記本電腦的datascienceexperience是因此您沒有root訪問權限。 –

回答

2

所以修復是相當簡單 - 我只是用在筆記本內核菜單項來重新啓動內核。

我在我的筆記本電腦上本地運行ipython時遇到了同樣的問題,解決方案是安裝tkinter,所以我沒有想到答案與重新啓動內核一樣簡單。

另一次我收到這個錯誤信息,重新啓動內核不起作用。我只好:

  • 變化火花後端
  • 下載筆記本文件
  • 刪除筆記本DSX
  • 創建從下載的筆記本
相關問題