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:
- The above code snippet gives an import error when using Python and IPython.
- The code snippet executes successfully in Jupyter.
Could someone explain why this is happening?
-
Possible duplicate of matplotlib error - no module named tkinterMaxPowers– MaxPowers2018年01月11日 08:13:41 +00:00Commented Jan 11, 2018 at 8:13
-
Please include tracebacks in your question instead of pics.MaxPowers– MaxPowers2018年01月11日 08:18:18 +00:00Commented 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).Stephen Jacob– Stephen Jacob2018年01月12日 00:38:50 +00:00Commented Jan 12, 2018 at 0:38
1 Answer 1
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.
2 Comments
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.Explore related questions
See similar questions with these tags.