【问题标题】:Matplotlib, Jupyter Notebook: ImportError: No module named TkinterMatplotlib,Jupyter Notebook:ImportError:没有名为 Tkinter 的模块
【发布时间】:2017-07-31 17:10:13
【问题描述】:

这个问题与ImportError: No module named 'Tkinter'不同,投反对票前请仔细阅读!

环境:

  • Python 2.7
  • CentOS 7
  • matplotlib 1.5.3
  • 笔记本4.1.0

安装:

install it by pip

显示我的代码

import gzip, binascii, struct, numpy
import matplotlib.pyplot as plt

with gzip.open(test_data_filename) as f:
    # Print the header fields.
    for field in ['magic number', 'image count', 'rows', 'columns']:
        # struct.unpack reads the binary data provided by f.read.
        # The format string '>i' decodes a big-endian integer, which
        # is the encoding of the data.
        print(field, struct.unpack('>i', f.read(4))[0])

    # Read the first 28x28 set of pixel values. 
    # Each pixel is one byte, [0, 255], a uint8.
    buf = f.read(28 * 28)
    image = numpy.frombuffer(buf, dtype=numpy.uint8)

    # Print the first few values of image.
    print('First 10 pixels:', image[:10])

显示错误

ImportErrorTraceback (most recent call last)
<ipython-input-9-8ba574e10b9a> in <module>()
      3 
      4 import gzip, binascii, struct, numpy
----> 5 import matplotlib.pyplot as plt
      6 
      7 

/usr/lib64/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

/usr/lib64/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

/usr/lib64/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 

/usr/lib64/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

/usr/lib64/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):

/usr/lib64/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 

ImportError: No module named Tkinter

TKinter

Python 2.7.5 (default, Sep 15 2016, 22:37:39) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named Tkinter
>>> 

这是怎么发生的

有一个Jupyter Notebook Server 在远程CentOS服务器上运行a,我通过本地网络浏览器访问笔记本,当我在jupyter笔记本中输入上述代码时,出现错误!!!

如何修复这个错误?谢谢!

【问题讨论】:

    标签: python matplotlib tkinter jupyter-notebook


    【解决方案1】:

    Matplotlib 可以使用多个backends,其中一些需要 GUI 工具包。看起来你的 matplotlib 安装默认配置为使用 TkAgg 后端。

    通常服务器没有安装 GUI 工具包(无论如何也没有多大意义),所以 matplotlib 应该配置为使用非 GUI 后端。例如,您可以在matplotlibrc 文件中指定backend : Agg(有关详细信息,请参阅上面的链接)。

    如果您无法在服务器上执行此操作,您可以在与笔记本相同的目录中创建自定义 matplotlibrc。或者只是在您的代码中设置后端。在笔记本中,这应该使您的绘图与 TkAgg 后端相同,并且不需要 Tkinter:

    import matplotlib as mpl
    mpl.use('Agg')
    import matplotlib.pyplot as plt
    

    【讨论】:

    • 感谢您的回答,但目前,解决我的问题的最简单方法是在我的 CentOS 机器上安装 Tkinter(或 tkinter)。
    【解决方案2】:

    这是一个简单但令人烦恼的问题。默认情况下,Tkinter(或tkinter)不会安装在 Linux 默认 Python 的包目录中。所以,在 CentOS 上,只需安装 Tkinter

    yum -y install tkinter
    

    当我尝试import Tkinter 时,出现导入错误。所以,我确定Tkinter(或tkinter) 包没有安装。

    更新

    这是另一个问题,它也有助于解决我的问题。

    点击python3-importerror-no-module-named-tkinter-on-ubuntu

    【讨论】:

    • @Goyo,是的,没错,但我的回答解决了我的问题。
    • 删除了我之前的评论,因为在您最后一次编辑之后,它没有任何意义。我认为切换到非交互式后端比安装 Tkinter 和它随身携带的所有 X11 东西要好得多,而且你永远不需要。
    猜你喜欢
    • 2017-07-08
    • 2018-06-20
    • 2016-07-19
    • 2018-02-09
    • 1970-01-01
    • 2016-04-30
    • 2017-01-09
    • 2016-02-26
    相关资源
    最近更新 更多