Why does Python installed via Homebrew not include 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.
Solution 4
With brew and python3 you have to install Tinker separately.
brew message while installing python:
tkinter is no longer included with this formula, but it is available separately:
brew install [email protected]
Solution 5
If you're using pyenv
you can try installing tcl-tk
via homebrew and then activating the env. vars. mentioned in its caveats section, as detailed in this answer. Activating those env. vars. prior to installing python via homebrew may work for you:
※ export PATH="/usr/local/opt/tcl-tk/bin:$PATH"
※ export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
※ export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
※ export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
※ export PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I$(brew --prefix tcl-tk)/include' \
--with-tcltk-libs='-L$(brew --prefix tcl-tk)/lib -ltcl8.6 -ltk8.6'"
※ brew reinstall python
Daniel Chen
Updated on July 09, 2022Comments
-
Daniel Chen almost 2 years
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
-
Daniel Chen about 8 yearsThanks, I've installed ActiveTcl either build Tcl/Tk by myself, but still got the same error.
-
Jacktose over 7 yearsI've done the same and reinstalled Python, and have the same error. I'm guessing there's a way to get
brew install
to use a different TCL, but I don't know it. There is--with-brewed-tk
, but is there a--with-activetcl-at-path ________
or something? -
Reblochon Masque over 7 yearsSorry, I can't help you, IDK.
-
hypnoglow almost 7 yearsYou need to use
--with-tcl-tk
instead of--use-brewed-tk
-
studgeek almost 7 yearsThe command to install xocde tools is
xcode-select --install
. Also,homebrew/dupes
is deprecated. Can just usebrew install tcl-tk
. -
fralau about 6 yearsIf you want this to work, you need to apply the advice by JBallin:
brew install python --with-tcl-tk
-
JBallin about 6 years@fralau I'm not sure how I left that out! Updated my answer, thanks!
-
fralau about 6 yearsYou're very welcome. If we were not allowed to leave anything out, we would never contribute in the first place!
-
Josh almost 6 yearsThis worked with LinuxBrew too! (minus the xcode-select step)
-
pjs over 5 yearsThis no longer seems to work. I'm getting the message
Warning: python: this formula has no --with-tcl-tk option so it will be ignored!
. -
Garini over 5 yearsThis is not working anymore as the optional
--with-tcl-tk
disappeared. -
lab about 5 yearsNote that the version with tkinter is now default: discourse.brew.sh/t/python3-installation-with-tkinter/3636/8 a force reinstall might be needed to get it though.
-
JBallin about 5 years@lab I tried
brew reinstall python3
andidle3
still shows the warning. Did you installtcl-tk
separately and somehow tell brew'spython3
to use that instead of system? -
rain01 about 5 yearsI'm not getting any warning, I just get an error now:
Error: invalid option: --with-tcl-tk
-
MRX over 4 years
--with-tcl-tk
is not working as mentioned by @pjs -
Daniel Gonzalez about 4 years
--with-tcl-tk
was removed unfortunately, you can see here. For my particular system, runningbrew uninstall --ignore-dependencies python && brew install python
did the trick. Unclear now why that worked, but it worked. I imagine it has something to do with rebuilding and linking stuff and other magic. -
Ken Shirriff about 4 yearsAfter trying many other solutions, the script above fixed the problem for me on macOS Catalina with python3. Now I can use matplotlib.
-
flow2k about 3 years...and here is the doc: formulae.brew.sh/formula/[email protected]
-
juanfal about 3 yearsThanks a lot… getting crazy with this. No info at all anywhere. It did the magic
brew install python-tk
-
Vikas Pandey almost 3 years
brew install python-tk
this one worked. thanks @sgon00 -
PeterK almost 3 yearsGreat, for python 3.9. The recipe is
brew install [email protected]
. Any idea how to do this for python 3.8? -
Near Future almost 3 yearsthanks, that one helped here, how should i search such info for the future from official docs ? ("brew tkinter" pointed me here)