Skip to content

resolved No module named _tkinter of matplotlib


matplotlib:2.0.0  (pip install matplotlib)

Error Message

>>> import matplotlib.pyplot
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/opt/soft/python2/lib/python2.7/site-packages/matplotlib/", line 115, in <module>
 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
 File "/opt/soft/python2/lib/python2.7/site-packages/matplotlib/backends/", line 32, in pylab_setup
 File "/opt/soft/python2/lib/python2.7/site-packages/matplotlib/backends/", line 6, in <module>
 from six.moves import tkinter as Tk
 File "/opt/soft/python2/lib/python2.7/site-packages/", line 203, in load_module
 mod = mod._resolve()
 File "/opt/soft/python2/lib/python2.7/site-packages/", line 115, in _resolve
 return _import_module(self.mod)
 File "/opt/soft/python2/lib/python2.7/site-packages/", line 82, in _import_module
 File "/opt/soft/python2/lib/python2.7/lib-tk/", line 39, in <module>
 import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

The solution:Recompile python

  1. Extract the source file
     [root@arno Python-2.7.13]# tar xzf Python-2.7.13.tgz
     [root@arno Python-2.7.13]# cd Python-2.7.13
  2. Check the tk/tcl version,Step 4 need the version information
     [root@arno Python-2.7.13]# rpm -qa | grep ^tk
     [root@arno Python-2.7.13]# rpm -qa | grep ^tcl
  3. Install tk/tcl devel
     [root@arno Python-2.7.13]# yum install tk-devel tcl-devel -y
  4. Find these lines and cancel the comment
     [root@arno Python-2.7.13]# vim Modules/Setup
     # *** Always uncomment this (leave the leading underscore in!):
     _tkinter _tkinter.c tkappinit.c -DWITH_APPINIT \
     -L/usr/local/lib \
     -I/usr/local/include \
     -ltk8.5 -ltcl8.5 \           # this version is query at step 2.
  5. Start complie & install python
     [root@arno Python-2.7.13]# ldconfig
     [root@arno Python-2.7.13]# ./configure --prefix=/opt/soft/python2 --with-ensurepip=install
     [root@arno Python-2.7.13]# make
     [root@arno Python-2.7.13]# make install
  6. Config sys env.
    [root@arno Python-2.7.13]# mv /usr/bin/python /usr/bin/pythonbak
    [root@arno Python-2.7.13]# unlink /usr/bin/python2
    [root@arno Python-2.7.13]# vim /usr/bin/yum
    [root@arno Python-2.7.13]# vim ~/.bash_profile
     # User specific environment and startup programs
     export PATH
    [root@arno Python-2.7.13]# source ~/.bash_profile
  7. Verification result
     Python 2.7.13 (default, Feb 20 2017, 20:35:07)
     [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
     Type "help", "copyright", "credits" or "license" for more information.
     >>> import matplotlib.pyplot
Published inPython

One Comment


电子邮件地址不会被公开。 必填项已用*标注