Skip to content

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

Closed

Description

@SamusAranX

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.

Activity

  1. tacaswell commented on Aug 10, 2017

    @tacaswell
    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!

  2. added this to the milestone on Aug 10, 2017
  3. SamusAranX commented on Aug 10, 2017

    @SamusAranX
    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.

  4. tacaswell commented on Aug 10, 2017

    @tacaswell
    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.

  5. WeatherGod commented on Aug 11, 2017

    @WeatherGod
    Member
  6. anntzer commented on Aug 17, 2017

    @anntzer
    Contributor

    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.

  7. dimaqq commented on Dec 7, 2017

    @dimaqq

    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.

  8. jklymak commented on Dec 7, 2017

    @jklymak
    Member

    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.

  9. dimaqq commented on Dec 7, 2017

    @dimaqq

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

  10. WeatherGod commented on Dec 7, 2017

    @WeatherGod
    Member
  11. shenjiefeng commented on Feb 23, 2018

    @shenjiefeng

    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

  12. tacaswell commented on Feb 23, 2018

    @tacaswell
    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).

  13. CarlWilson111 commented on Sep 18, 2018

    @CarlWilson111

    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.

  14. dimaqq commented on Sep 18, 2018

    @dimaqq

    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.

  15. jklymak commented on Sep 18, 2018

    @jklymak
    Member

    3.0.0 will change the backend fallback behaviour.

  16. tacaswell commented on Sep 18, 2018

    @tacaswell
    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.

  17. dimaqq commented on Sep 19, 2018

    @dimaqq

    Thanks @jklymak that's very good to hear!

  18. sajed-zarrinpour commented on Apr 11, 2019

    @sajed-zarrinpour

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

  19. sajed-zarrinpour commented on Nov 10, 2019

    @sajed-zarrinpour
  20. KipCrossing commented on May 29, 2020

    @KipCrossing

    lt;dr?
    upgrade to >3.0.0

  21. gosuto-inzasheru commented on Jun 2, 2020

    @gosuto-inzasheru

    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.

  22. jklymak commented on Jun 2, 2020

    @jklymak
    Member

    @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.

  23. tacaswell commented on Jun 2, 2020

    @tacaswell
    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.

  24. phoenixeliot commented on Sep 16, 2020

    @phoenixeliot

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions