0

I have installed python 2.7.10 in CentOS 6.5. Then I installed matplotlib using pip. However, I am getting the following error when trying to import matplotlib.pyplot.

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

tkinter seems to be installed.
"Package tkinter-2.6.6-66.el6_8.x86_64 already installed and latest version"

I have _tkinter.so module in python2.6/lib-dynload, but nothing related to tkinter under python2.7/lib-dynload.

/usr/lib64/python2.6/lib-dynload/_tkinter.so
/usr/local/lib/python2.7/lib-tk/test/test_tkinter

Also, I verified sys.path in python to get the following:

Python 2.7.10 (default, Jun  1 2017, 18:51:46)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print '\n'.join(sys.path)

/usr/local/lib/python27.zip  
/usr/local/lib/python2.7  
/usr/local/lib/python2.7/plat-linux2  
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old  
/usr/local/lib/python2.7/lib-dynload  
/usr/local/lib/python2.7/site-packages
proutray
  • 1,943
  • 3
  • 30
  • 48
  • 1
    How did you get Python 2.7 on CentOS 6? Or, if you don't need a UI, you can change your backend to just render PNGs/PDFs with `import matplotlib;matplotlib.use('agg')` before other imports (see [What is a backend?](http://matplotlib.org/faq/usage_faq.html#what-is-a-backend)). – Nick T Jun 01 '17 at 23:29
  • @NickT - I have manually installed 2.7.10. I have followed a few sources but mostly this: http://blog.ruterly.com/2015/12/03/Update-python/ – proutray Jun 02 '17 at 00:39
  • 1
    https://stackoverflow.com/questions/5459444/tkinter-python-may-not-be-configured-for-tk Check this out. This explains what wrong with unable to import tkinter – crazyglasses Jun 02 '17 at 03:14

2 Answers2

0

This may be a problem with your python library path. Check to see where tkinter is installed, and make sure that you have that available to your Python interpreter. Can you import anything else from tkinter?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • I have added some more details which might be relevant to what you have asked. Thanks. – proutray Jun 01 '17 at 23:26
  • May be your both python2.6 and python2.7 have packages, which are not compatible with each other.Why dont you uninstall python2.6,this may solve your problem .Hope this helps. – blitu12345 Jun 02 '17 at 08:13
0

Try to install tkinter as they said ”no module named _tkinter“. You can do so with that command:

sudo apt-get install python-tk
Utopia
  • 278
  • 1
  • 3
  • 19
  • 1
    `apt-get` is for Debian-likes, while he has CentOS 6 that'll be using `yum` and has different package names – Nick T Jun 01 '17 at 23:30
  • @utopia - as mentioned in the details, tkinter seems to be installed. "Package tkinter-2.6.6-66.el6_8.x86_64 already installed and latest version" – proutray Jun 02 '17 at 00:41