1

The current version of Python3 is 3.5.2 when I import matplotlib it retuned the following error

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/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.5/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/lib/python3.5/site-packages/six.py", line 92, in __get__
    result = self._resolve()
  File "/usr/local/lib/python3.5/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python3.5/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'

And import tkinter

Python 3.5.2 (default, Jan 19 2017, 11:29:22)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
>>>

It seems that tkinter has already been intalled.

I have installed tk and tcl by

sudo apt-get install tk-dev
sudo apt-get install tk8.6-dev

And OS is Ubuntu 14.04.

I think it is the reason that TK was not configured on Python3, but I'm not sure. Many people said I should rebuild and reinstall Python3 with tk, however I don't think it is an elegant way to solve this problem.

How can I fix this problem?

GoingMyWay
  • 16,802
  • 32
  • 96
  • 149
  • Have you tried installing it with `pip`? – moritzg May 29 '17 at 07:57
  • @moritzg, `Requirement already satisfied: tkinter in /usr/local/lib/python3.5/site-packages` – GoingMyWay May 29 '17 at 07:59
  • Ok what about `sudo apt-get install python3-tk`? – moritzg May 29 '17 at 08:01
  • @moritzg, it has been already installed before – GoingMyWay May 29 '17 at 08:03
  • 1
    check your path.. your error `"/usr/local/lib/python3.5/tkinter/__init__.py"` and your installation `Requirement already satisfied: tkinter in /usr/local/lib/python3.5/site-packages` instead of site-packages it is trying to find it in python3.5 config your path for tkinter again – Gahan May 29 '17 at 08:06
  • Checkout the steps on the bottom of this [page](https://wiki.python.org/moin/TkInter) – moritzg May 29 '17 at 08:06
  • @moritzg, thx your page, however it's very ambiguous for how to configure TK for Python3.5 – GoingMyWay May 29 '17 at 08:23
  • @moritzg `tkinter` is part of the standard library. It **cannot be installed using Pip, which should not be tried for any standard library component**. – Karl Knechtel Apr 28 '23 at 20:56

1 Answers1

3

If you are having trouble with a matplotlib backend try selecting a different one.
Matplotlib caters for many different scenarios and uses.
On Linux, I use the following code to select whichever backend is available and works first.

import matplotlib
gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg']
for gui in gui_env:
    try:
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        break
    except:
        continue

or if you are going to be creating an image file rather than displaying it

Use:

matplotlib.use('agg')
from matplotlib import pyplot as plt

Edit:
Based on your comments try this and see if you get a result that works.

import matplotlib
gui_env = [i for i in matplotlib.rcsetup.interactive_bk]
print ("I will test for", gui_env)
for gui in gui_env:
    print ("testing", gui)
    try:
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        print ("    ",gui, "Is Available")
        plt.plot([1.5,2.0,2.5])
        fig = plt.gcf()
        fig.suptitle(gui)
        plt.show()
        print ("Using ..... ",matplotlib.get_backend())
    except:
        print ("    ",gui, "Not found")
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Thx, however, it returned `ImportError: No module named 'wx' .... ImportError: Matplotlib backend_wx and backend_wxagg require wxPython >=2.8.12` – GoingMyWay May 29 '17 at 16:12
  • In other words, you have got not a single one of the 4 common interactive backends tested for, on your system! You are running Linux aren't you? – Rolf of Saxony May 30 '17 at 07:12
  • Yes, Ubuntu Server 14.04. – GoingMyWay May 30 '17 at 09:59
  • 'WebAgg', 'nbAgg' are available. – GoingMyWay May 30 '17 at 14:29
  • That's some crazy linux box you have there, if those are the only 2 available – Rolf of Saxony May 30 '17 at 15:47
  • I am not in a position to tell you why. It will be something 'odd' about either your OS set up or the environment that you are running this under. At least you now have an option :) Try installing wxpython and see if that is still not working, that would point at the environment. – Rolf of Saxony May 31 '17 at 08:01