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?

8
  • Have you tried installing it with pip? Commented May 29, 2017 at 7:57
  • @moritzg, Requirement already satisfied: tkinter in /usr/local/lib/python3.5/site-packages Commented May 29, 2017 at 7:59
  • Ok what about sudo apt-get install python3-tk? Commented May 29, 2017 at 8:01
  • @moritzg, it has been already installed before Commented May 29, 2017 at 8: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 Commented May 29, 2017 at 8:06

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

6 Comments

Thx, however, it returned ImportError: No module named 'wx' .... ImportError: Matplotlib backend_wx and backend_wxagg require wxPython >=2.8.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?
Yes, Ubuntu Server 14.04.
'WebAgg', 'nbAgg' are available.
That's some crazy linux box you have there, if those are the only 2 available
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.