81

I've installed Python via Homebrew on my Mac.

brew install python

After that, I checked my Python version as 2.7.11, and then I tried to perform

import Tkinter

I got the 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
0

10 Answers 10

87

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot… getting crazy with this. No info at all anywhere. It did the magic brew install python-tk
brew install python-tk this one worked. thanks @sgon00
Great, for python 3.9. The recipe is brew install [email protected]. Any idea how to do this for python 3.8?
thanks, that one helped here, how should i search such info for the future from official docs ? ("brew tkinter" pointed me here)
I needed to use: env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.13 on MacOS to reinstall my copy of python after installing python-tk to get it to work, not sure why (found here)
26

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

5 Comments

The command to install xocde tools is xcode-select --install. Also, homebrew/dupes is deprecated. Can just use brew install tcl-tk.
This worked with LinuxBrew too! (minus the xcode-select step)
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!.
I'm not getting any warning, I just get an error now: Error: invalid option: --with-tcl-tk
--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.
14

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]

2 Comments

This works ok for me with macOS Catalina
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.

6 Comments

If you want this to work, you need to apply the advice by JBallin: brew install python --with-tcl-tk
@fralau I'm not sure how I left that out! Updated my answer, thanks!
You're very welcome. If we were not allowed to leave anything out, we would never contribute in the first place!
This is not working anymore as the optional --with-tcl-tk disappeared.
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.
|
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

Comments

8

On Mac OS X, you must install Tcl separately:

You will find instructions and dowloadables on Tcl/Tk Software and Apple Macintosh and Tcl/Tk.

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

3 Comments

Thanks, I've installed ActiveTcl either build Tcl/Tk by myself, but still got the same error.
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?
Sorry, I can't help you, IDK.
5

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  

Comments

3

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

xcode-select --install
brew uninstall python
brew install python --use-brewed-tk

2 Comments

You need to use --with-tcl-tk instead of --use-brewed-tk
--with-tcl-tk is not working as mentioned by @pjs
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

1 Comment

After trying many other solutions, the script above fixed the problem for me on macOS Catalina with python3. Now I can use matplotlib.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.