Why my Python installed via home brew not include Tkinter

Daniel Chen picture Daniel Chen · Apr 21, 2016 · Viewed 32.8k times · Source

I've installed my Python via homebrew on Mac.

brew install python

and after that I've checked my python version as 2.7.11, then I've tried perform

import Tkinter

I've got following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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

Answer

jalairo picture jalairo · Dec 19, 2016

Based on the comments from above and the fact that Python must be linked to Tcl/Tk framework:

If you don't have Xcode command line tools, install those:

xcode-select --install

If you don't have Tcl/Tk brew installation (check brew list), install that:

brew install tcl-tk

Then, run "brew uninstall python" if that was not installed with option --with-tcl-tk (the current official option). Then install Python again, linking it to the brew installed Tcl/Tk:

brew install python --with-tcl-tk