Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Getting "No module named '_tkinter'" error #9017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SamusAranX opened this issue Aug 10, 2017 · 23 comments
Closed

Getting "No module named '_tkinter'" error #9017

SamusAranX opened this issue Aug 10, 2017 · 23 comments

Comments

@SamusAranX
Copy link

SamusAranX commented Aug 10, 2017

Bug report

I just wanted to use matplotlib to draw a line graph and save it to a file, but every time I try to import the pyplot module I get the abovementioned error message.

python3 -c "import matplotlib.pyplot"

Actual outcome

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

Expected outcome

Whatever happens when import matplotlib.pyplot succeeds

Matplotlib version

  • Operating System: Windows Subsystem for Linux
  • Matplotlib Version: 2.0.2
  • Python Version: 3.6.0

I installed matplotlib using pip.
Doing sudo apt-get install python3-tk, as recommended by someone on Stack Overflow, didn't help.

@tacaswell
Copy link
Member

How did you install mpl?

Can you do import tkinter in a python shell?

If you built from source suspect you also need the tk dev packages as well?

This is the first report (that I can recall) of using Matplotilb in Windows Subsystem for Linux, you may be a guinea pig 😅 Any notes from you experience with this that could go in the docs would be very appreciated!

@tacaswell tacaswell added this to the 2.1.1 (next bug fix release) milestone Aug 10, 2017
@SamusAranX
Copy link
Author

I installed matplotlib using pip.

😛

I installed Python 3.6 from source though, omitting the tk packages as I didn't need them.
Guess I'll try that tomorrow while upgrading to 3.6.2.

@tacaswell
Copy link
Member

Oh, that is easy to fix then

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

If you didn't install tk in your python then you can not use the tk backend.

@WeatherGod
Copy link
Member

WeatherGod commented Aug 11, 2017 via email

@anntzer
Copy link
Contributor

anntzer commented Aug 17, 2017

Closing as this is apparently a python install issue -- but feel free to request a reopen if something else is wrong, or to suggest some change in the docs.

@anntzer anntzer closed this as completed Aug 17, 2017
@dimaqq
Copy link

dimaqq commented Dec 7, 2017

My 2c: should be reopened for the following reason:

from matplotlib import pyplot as plt
ModuleNotFoundError: No module named '_tkinter'
import matplotlib
matplotlib.use("agg")
UserWarning: 
This call to matplotlib.use() has no effect...
from matplotlib import pyplot as plt
ModuleNotFoundError: No module named '_tkinter'

First error is fine, but the second error is not.
IMO it's not OK that state is messed up and user cannot recover.

@jklymak
Copy link
Member

jklymak commented Dec 7, 2017

Pyplot sets the backend if it’s lot been set already. The user only need move the import matpltolib and matplotlib.use before the pyplot import.

@dimaqq
Copy link

dimaqq commented Dec 7, 2017

That's exactly the problem -- requiring user to do something before they know they need it.

@WeatherGod
Copy link
Member

WeatherGod commented Dec 7, 2017 via email

@shenjiefeng
Copy link

on minimal install centos7.

first

yum install -y tkinter tk-devel

then in ipython env,

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt # no error, it works
plt.figure(1).show()

arisen:

/root/venv36/lib/python3.6/site-packages/matplotlib/figure.py:418: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
"matplotlib is currently using a non-GUI backend, "

I have already Configure X11 forwarding on xshell with the steps of http://www.doxer.org/use-xming-xshell-putty-tightvnc-to-display-linux-gui-on-windows-desktop-x11-forwarding-when-behind-firewall/

but it still not work

@tacaswell
Copy link
Member

@shenjiefeng with matplotlib.use('agg') you are explicitly asked for a non-GUI backend.

This question would be better handled on the mailing list [email protected] (https://mail.python.org/mailman/listinfo/matplotlib-users you will have to subscribe to post un-moderated).

bjmorgan added a commit to bjmorgan/DynaPhoPy that referenced this issue Feb 26, 2018
importing matplotlib on systems that do not have TkInter installed
gives a ModuleNotFoundError (see matplotlib/matplotlib#9017)
This commit works around this issue by setting the 'agg' backend
for matplotlib.
@CarlWilson111
Copy link

Oh, that is easy to fix then

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

If you didn't install tk in your python then you can not use the tk backend.

That is used for me.
In some Saas platforms, users are forbiden to change system environment, so fix like this will help.
Thank you very much.

@dimaqq
Copy link

dimaqq commented Sep 18, 2018

It's a total bummer that matplotlib initializes the backend early, and fails hard.
Perhaps there's a good reason for that.
From what I see, a notable share of users stumbles on this when trying matplotlib for the first time, i.e. if they copy-paste an example and boom! a cryptic exception instead of pretty plots.
I sincerely hope that the maintainers take a second look at this issue.

@jklymak
Copy link
Member

jklymak commented Sep 18, 2018

3.0.0 will change the backend fallback behaviour.

@tacaswell
Copy link
Member

From what I see, a notable share of users stumbles on this when trying matplotlib for the first time, i.e. if they copy-paste an example and boom! a cryptic exception instead of pretty plots.

The other option is no exceptions and no plots. The cryptic error at least suggests that something is not behaving as expected.

@dimaqq
Copy link

dimaqq commented Sep 19, 2018

Thanks @jklymak that's very good to hear!

@sajed-zarrinpour
Copy link

in my case on manjaro Linux, installing python-pmw worked it out.

michaelmell added a commit to nimwegenLab/moma-preprocessing that referenced this issue May 17, 2019
…without tk or graphic interface.

* See here for discussion: matplotlib/matplotlib#9017 (comment)
* One can also work around this using by setting a text-based matplotlib backend like so:
    import matplotlib
    matplotlib.use('agg')
* Error message is like so:
  File "/home/micha/Documents/git/mmpreprocesspy/venv-testenv/lib/python3.7/site-packages/matplotlib/backends/_backend_tk.py", line 5, in <module>
    import tkinter as Tk
ModuleNotFoundError: No module named 'tkinter'
@sajed-zarrinpour
Copy link

sajed-zarrinpour commented Nov 10, 2019 via email

@KipCrossing
Copy link

lt;dr?
upgrade to >3.0.0

@gosuto-inzasheru
Copy link

I am still getting this on matplotlib=3.2.1. Also, why is tkinter not just a dependency of matplotlib? This is messing up package management for me.

@jklymak
Copy link
Member

jklymak commented Jun 2, 2020

@jorijnsmit Can you open a new issue that explains what you are trying to do. w/ 3.2.1 you should get a fallback to agg at the very least.

@tacaswell
Copy link
Member

@jorijnsmit We do not have a hard dependency on an GUI toolkit because there are many uses where you do not need it (batch jobs, use in a notebook, in a webserver etc).

The dependency on Tk is further complicated because the bindings are part of the standard library, but some packagers split it out into a separate package. If you think the Matplotliblib package should depend on the python-tk package, please take that up with the maintainers of your packaging tool.

@phoenixeliot
Copy link

I know this is an old issue, but I found the most reliable solution to this on macOS, especially in combination with the PyCharm debugger crashing due to matplotlib related tk issues, was to use the backend 'macosx'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

15 participants