67

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
john-hen
  • 4,410
  • 2
  • 23
  • 40
Daniel Chen
  • 1,933
  • 5
  • 24
  • 33

10 Answers10

71

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.

sgon00
  • 4,879
  • 1
  • 43
  • 59
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
JBallin
  • 8,481
  • 4
  • 46
  • 51
jalairo
  • 383
  • 3
  • 4
  • 1
    The command to install xocde tools is `xcode-select --install`. Also, `homebrew/dupes` is deprecated. Can just use `brew install tcl-tk`. – studgeek Jul 13 '17 at 15:58
  • This worked with LinuxBrew too! (minus the xcode-select step) – Josh Jul 12 '18 at 18:29
  • 14
    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!`. – pjs Dec 03 '18 at 17:46
  • 6
    I'm not getting any warning, I just get an error now: `Error: invalid option: --with-tcl-tk` – rain01 Apr 02 '19 at 19:58
  • 2
    `--with-tcl-tk` was removed unfortunately, you can see [here](https://github.com/Homebrew/homebrew-core/commit/a377978583a538110a1e6e2fd95530ec6f6e7abe). 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. – Daniel Gonzalez Feb 18 '20 at 04:32
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 python-tk@3.9
nash0rn
  • 271
  • 4
  • 5
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.

JBallin
  • 8,481
  • 4
  • 46
  • 51
  • If you want this to work, you need to apply the advice by JBallin: `brew install python --with-tcl-tk` – fralau Apr 27 '18 at 06:51
  • @fralau I'm not sure how I left that out! Updated my answer, thanks! – JBallin Apr 29 '18 at 00:50
  • You're very welcome. If we were not allowed to leave anything out, we would never contribute in the first place! – fralau Apr 30 '18 at 04:34
  • 7
    This is not working anymore as the optional `--with-tcl-tk` disappeared. – Garini Dec 04 '18 at 17:36
  • Note that the version with tkinter is now default: https://discourse.brew.sh/t/python3-installation-with-tkinter/3636/8 a force reinstall might be needed to get it though. – lab Mar 08 '19 at 13:13
  • @lab 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? – JBallin Mar 08 '19 at 17:03
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
Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
Carl G
  • 17,394
  • 14
  • 91
  • 115
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.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • 1
    Thanks, I've installed ActiveTcl either build Tcl/Tk by myself, but still got the same error. – Daniel Chen Apr 21 '16 at 07:07
  • 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? – Jacktose Sep 13 '16 at 02:29
  • Sorry, I can't help you, IDK. – Reblochon Masque Sep 13 '16 at 04:57
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
shi
  • 286
  • 1
  • 9
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

Csaba K.
  • 145
  • 1
  • 7
  • After trying many other solutions, the script above fixed the problem for me on macOS Catalina with python3. Now I can use matplotlib. – Ken Shirriff Mar 16 '20 at 22:51
2

This is what worked for me with macOS Monterey:

brew install python-tk@3.10 or brew install python-tk@3.9

Depending on which Python version you're running.

Qubit
  • 61
  • 6
2

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  
Iuri Guilherme
  • 389
  • 1
  • 10