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
  • 82,630
  • 20
  • 166
  • 161
Stephen Jacob
  • 889
  • 1
  • 15
  • 33
  • Possible duplicate of [matplotlib error - no module named tkinter](https://stackoverflow.com/questions/36327134/matplotlib-error-no-module-named-tkinter) – MaxPowers Jan 11 '18 at 08:13
  • Please include tracebacks in your question instead of pics. – MaxPowers Jan 11 '18 at 08: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). – Stephen Jacob Jan 12 '18 at 00:38

1 Answers1

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.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • 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. – Stephen Jacob Jan 12 '18 at 06:11
  • 1
    Updated my answer. – Mike Müller Jan 12 '18 at 12:04