ubuntu14.0环境下matplotlib中ImportError: No module named _tkinter问题解决

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sparkexpert/article/details/73729145

Matplotlib是一个Python下的图形框架,可以很方便地进行图形化显示。可以说是做图像处理和数据分析必装的库。

但是在实际应用过程中,经常发现有这样的错误,即:

import matplotlib.pyplot as plt

这样就会报出这样的错误。
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/lib/python3.6/site-packages/six.py", line 92, in __get__
    result = self._resolve()
  File "/usr/local/lib/python3.6/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python3.6/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'


针对这个问题,发现ubuntu的显示环境依赖的是tk,因此需要进行各种库的安装。

(适用于python3环境下)

sudo apt-get install tcl-dev tk-dev python3-tk

执行完上述命令之后,需要对python整个库再重新更新一下。即

对python的源码包再重新make ,  sudo make install一下。(此步骤非常关键。)

#!/usr/bin/env python  
import matplotlib.pyplot as plt  
  
plt.plot([10, 20, 30])  
plt.xlabel('tiems')  
plt.ylabel('numbers')  
plt.show() 

执行完上述测试语句,即可得到绘图效果。则证明已经可以成功。




展开阅读全文

没有更多推荐了,返回首页