2

I am struggling to make basic plots in matplotlib. It looks like I'm not installing tkinter correctly, which I understand should be installed from the --with-tcl-tk flag. What could be causing my error? How can I better diagnose this problem? Downloading the ActiveState tkinter has not helped me.

I uninstalled then reinstalled everything I thought was relevant. No errors were raised by brew.

$ brew uninstall python
$ brew uninstall matplotlib numpy --force
$ brew install python --with-tcl-tk
$ brew install numpy matplotlib --with-tcl-tk

However, I get an error when I import pyplot:

$ python
Python 2.7.11 (default, Dec 22 2015, 12:47:31) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from matplotlib import pyplot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 109, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/lib/python2.7/site-packages/six.py", line 199, in load_module
    mod = mod._resolve()
  File "/usr/local/lib/python2.7/site-packages/six.py", line 113, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python2.7/site-packages/six.py", line 80, in _import_module
    __import__(name)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
>>> 
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
kilojoules
  • 9,768
  • 18
  • 77
  • 149
  • Does this answer your question? [Why does tkinter (or turtle) seem to be missing or broken? Shouldn't it be part of the standard library?](https://stackoverflow.com/questions/76105218/why-does-tkinter-or-turtle-seem-to-be-missing-or-broken-shouldnt-it-be-part) – Karl Knechtel Apr 30 '23 at 00:32

2 Answers2

1

In my case I solved this by uninstalling and reinstalling Python using homebrew. I hope there is a better solution that someone else can provide...

$ brew uninstall python@2
Uninstalling /usr/local/Cellar/python@2/2.7.14_3... (4,324 files, 77.4MB)
$ brew install python@2
==> Downloading https://homebrew.bintray.com/bottles/python@2-2.7.14_3.el_capita
######################################################################## 100.0%
==> Pouring python@2-2.7.14_3.el_capitan.bottle.2.tar.gz
==> /usr/local/Cellar/python@2/2.7.14_3/bin/python -s setup.py --no-user-cfg ins
==> /usr/local/Cellar/python@2/2.7.14_3/bin/python -s setup.py --no-user-cfg ins
==> /usr/local/Cellar/python@2/2.7.14_3/bin/python -s setup.py --no-user-cfg ins
==> Caveats
Pip and setuptools have been installed. To update them
  pip install --upgrade pip setuptools

You can install Python packages with
  pip install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
  /usr/local/Cellar/python@2/2.7.14_3: 4,669 files, 82.4MB

Pip kept everything, and the venv I was using was also still intact.

user3.1415927
  • 367
  • 3
  • 19
1

This is a little faster (and hopefully safer) than the uninstall method...

1) Ensure you have Brew's Tcl/Tk installed (it is kept separate from the MacOS one)

brew install tcl-tk

2) Now recompile Python with Tcl/Tk enabled

brew reinstall python --with-tcl-tk
ewall
  • 27,179
  • 15
  • 70
  • 84
  • I think `brew reinstall python --with-tcl-tk` fails in my Homebrew 2.1.10 with tcl-tk 8.6.9. Is it possible? – 1737973 Aug 18 '19 at 06:18