Why does Python installed via Homebrew not include Tkinter [duplicate]
Asked Answered
W

10

69

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
Worship answered 21/4, 2016 at 5:59 Comment(0)
B
74

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.

Batavia answered 2/4, 2021 at 13:14 Comment(4)
Thanks a lot… getting crazy with this. No info at all anywhere. It did the magic brew install python-tkPerpetua
brew install python-tk this one worked. thanks @BataviaClerkly
Great, for python 3.9. The recipe is brew install [email protected]. Any idea how to do this for python 3.8?Headdress
thanks, that one helped here, how should i search such info for the future from official docs ? ("brew tkinter" pointed me here)Law
L
25

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
Loralorain answered 19/12, 2016 at 14:1 Comment(5)
The command to install xocde tools is xcode-select --install. Also, homebrew/dupes is deprecated. Can just use brew install tcl-tk.Goodwife
This worked with LinuxBrew too! (minus the xcode-select step)Lombroso
This 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!.Obtrude
I'm not getting any warning, I just get an error now: Error: invalid option: --with-tcl-tkPhotocomposition
--with-tcl-tk was removed unfortunately, you can see here. For my particular system, running brew 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.Burge
R
13

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]
Reclaim answered 13/4, 2021 at 16:19 Comment(2)
...and here is the doc: formulae.brew.sh/formula/[email protected]Suavity
This works ok for me with macOS CatalinaJohny
P
12

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.

Poaceous answered 12/2, 2018 at 0:22 Comment(6)
If you want this to work, you need to apply the advice by JBallin: brew install python --with-tcl-tkCardona
@Cardona I'm not sure how I left that out! Updated my answer, thanks!Poaceous
You're very welcome. If we were not allowed to leave anything out, we would never contribute in the first place!Cardona
This is not working anymore as the optional --with-tcl-tk disappeared.Annia
Note 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.Shillong
@Shillong I tried brew reinstall python3 and idle3 still shows the warning. Did you install tcl-tk separately and somehow tell brew's python3 to use that instead of system?Poaceous
C
9

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
Carduaceous answered 29/2, 2020 at 20:16 Comment(0)
C
7

On mac OSX you must install TCL separately:

You will find instructions and dowloadables here: https://www.tcl.tk/software/tcltk/ and there: http://wiki.tcl.tk/1013

It requires a little bit of effort, but it is neither complicated nor difficult.

Cupreous answered 21/4, 2016 at 6:3 Comment(3)
Thanks, I've installed ActiveTcl either build Tcl/Tk by myself, but still got the same error.Worship
I'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?Wilterdink
Sorry, I can't help you, IDK.Cupreous
A
3

It may be because you don't have the latest Xcode command line tools so brew built python from source instead of from bottle. Try:

xcode-select --install
brew uninstall python
brew install python --use-brewed-tk
Absher answered 17/10, 2016 at 10:26 Comment(2)
You need to use --with-tcl-tk instead of --use-brewed-tkUnderclassman
--with-tcl-tk is not working as mentioned by @ObtrudeBusby
N
3

It doesn't work in any OS whatsoever that doesn't have the TCL Toolkit already installed. While it's either already installed in many Linux distributions and/or bundled with Python bundles downloaded from python.org for Windows and Linux - and a a consequence of that is generally wrongly assumed that it's part of Python - it's not the case for macOS. There's official reasons for this described in the appropriate document:

If you are using macOS 12 Monterey or later, you may see problems with file open and save dialogs when using IDLE or other tkinter-based applications. The most recent versions of python.org installers (for 3.10.0 and 3.9.8) have patched versions of Tk to avoid these problems. They should be fixed in an upcoming Tk 8.6.12 release.

If you are using a Python from any current python.org Python installer for macOS (3.10.0+ or 3.9.0+), no further action is needed to use IDLE or tkinter. A built-in version of Tcl/Tk 8.6 will be used.

If you are using macOS 10.6 or later, the Apple-supplied Tcl/Tk 8.5 has serious bugs that can cause application crashes. If you wish to use IDLE or Tkinter, do not use the Apple-supplied Pythons. Instead, install and use a newer version of Python from python.org or a third-party distributor that supplies or links with a newer version of Tcl/Tk.

Python's integrated development environment, IDLE, and the tkinter GUI toolkit it uses, depend on the Tk GUI toolkit which is not part of Python itself. For best results, it is important that the proper release of Tcl/Tk is installed on your machine. For recent Python installers for macOS downloadable from this website, here is a summary of current recommendations followed by more detailed information.


It has been already mentioned but the most popular way to do it is:

$ brew install python-tk 

It will work because python-tk formulae depends on other two: python and tcl-tk (hence you don't need to additionally do brew install python).

If you had already installed python with homebrew

$ brew install python  

You can have tkinter with

$ brew install tcl-tk  
Nefertiti answered 2/12, 2022 at 20:40 Comment(0)
S
2

It is a bit more complicated now, true you still need to have xcode command line tools and homebrew as a start. But the procedure changes constantly. Homebrew took out tcl-tk support long ago, and apple still only supplies v8.5 of tcl-tk. Anyway, it is possible, and I maintain a github gist personally to fix these issues.

Latest update is using python 3.8.1 (will probably be usable on the 3.8.x branch later too) see here, just follow the steps outlined. github gist link to install tcl-tk with python

Stilly answered 16/1, 2020 at 6:7 Comment(1)
After trying many other solutions, the script above fixed the problem for me on macOS Catalina with python3. Now I can use matplotlib.Coin
F
2

This is what worked for me with macOS Monterey:

brew install [email protected] or brew install [email protected]

Depending on which Python version you're running.

Flocculus answered 13/8, 2022 at 23:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.