0

I am using Python 3.5 within a program called OpenQuake. When I execute the following line, I get an import error "No module name 'tkinter'".

import matplotlib.pyplot as plt

I installed jupyter and executed the same and it works fine. Following is a snapshot. enter image description here I rechecked using IPython (which I believe comes with jupyter) to get exactly the same original error with the same traceback. Following is the traceback.

Traceback (most recent call last):
 File "<ipython-input-22-964337a9f103>", line 2, in <module>
 import matplotlib.pyplot as plt
 File "C:\Program Files\OpenQuake Engine\lib\site-packages\matplotlib\pyplot.py", line 114, in <module>
 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
 File "C:\Program Files\OpenQuake Engine\lib\site-packages\matplotlib\backends\__init__.py", line 32, in pylab_setup
 globals(),locals(),[backend_name],0)
 File "C:\Program Files\OpenQuake Engine\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 6, in <module>
 from matplotlib.externals.six.moves import tkinter as Tk
 File "C:\Program Files\OpenQuake Engine\lib\site-packages\matplotlib\externals\six.py", line 90, in __get__
 result = self._resolve()
 File "C:\Program Files\OpenQuake Engine\lib\site-packages\matplotlib\externals\six.py", line 113, in _resolve
 return _import_module(self.mod)
 File "C:\Program Files\OpenQuake Engine\lib\site-packages\matplotlib\externals\six.py", line 80, in _import_module
 __import__(name)
ImportError: No module named 'tkinter'

The matplotlib module that is referenced is the same in both the cases.

To summarize:

  1. The above code snippet gives an import error when using Python and IPython.
  2. The code snippet executes successfully in Jupyter.

Could someone explain why this is happening?

Mike Müller
86k20 gold badges174 silver badges164 bronze badges
asked Jan 11, 2018 at 3:57
3
  • Possible duplicate of matplotlib error - no module named tkinter Commented Jan 11, 2018 at 8:13
  • Please include tracebacks in your question instead of pics. Commented Jan 11, 2018 at 8:18
  • @MaxPowers As mentioned execution works in Jupyter but not in Ipython or Python so I do not think this is a duplicate of that question (which has to do with installation). Commented Jan 12, 2018 at 0:38

1 Answer 1

2

matplotlib uses different backends. In Jupyter it typically uses inline or notebook. On the other hand, IPython uses the TK backend by default (backend_tkagg.py) and therefore tries to import tkinter, which is not installed. Jupyter does not need this backend and therefore does not try to import it.

Jupyter displays the plot results in the browser but IPython runs on the terminal and needs a GUI library such as TKinter for display. This makes different backbends necessary.

answered Jan 12, 2018 at 1:22

2 Comments

Could you elaborate on what you mean by "backend_tkagg.py was not installed" since the file is present and it shows in the trace back. Based on your answer I did determine that matplotlib backend for Ipython is TkAgg and for Jupyter is module://ipykernel.pylab.backend_inline. I am layman when it comes to how this is developed but why can't ipython use the same module://ipykernel.pylab.backend_inline? If you can kindly advice me or point me in the right direction.
Updated my answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.