Why does Python installed via Homebrew not include Tkinter
I've installed Python via Homebrew on my Mac.
brew install python
After that I checked my Python version as 2.7.11, then I tried to perform
import Tkinter
I 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
Solution 1:
I am running MacOS Big Sur (11.2.3).
With python2, I have Tkinter
built-in.
With python3, it has to be installed manually and it's very simple, just run:
$ brew install python-tk
To run python2 in a terminal, execute python file.py
.
To run python3 in a terminal, execute python3 file.py
.
Solution 2:
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
Solution 3:
UPDATE: Other answers have found workarounds, so this answer is now outdated.
12/18 Update: No longer possible for various reasons.
Below is now outdated. You'll have to install Python directly from python.org if you want to remove those warnings.
2018 Update
brew reinstall python --with-tcl-tk
Note: Homebrew now uses Python 3 by default - Homebrew Blog. Docs.
Testing
python
should bring up system’s Python 2, python3
should bring up Python 3.
idle
points to system Python/tcl-tk. It will show an out-dated tcl-tk error (unless you brew install python@2 --with-tcl-tk
)
idle3
should bring up Python 3 with no warnings.
Caveat
--with-tcl-tk
will install python
directly from python.org, which you'll see when you run brew info python
.
More info here.