Why does Python installed via Homebrew not include Tkinter

50,753

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
Share:
50,753
Daniel Chen
Author by

Daniel Chen

Updated on July 09, 2022

Comments

  • Daniel Chen
    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
    Daniel Chen about 8 years
    Thanks, I've installed ActiveTcl either build Tcl/Tk by myself, but still got the same error.
  • Jacktose
    Jacktose over 7 years
    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?
  • Reblochon Masque
    Reblochon Masque over 7 years
    Sorry, I can't help you, IDK.
  • hypnoglow
    hypnoglow almost 7 years
    You need to use --with-tcl-tk instead of --use-brewed-tk
  • studgeek
    studgeek almost 7 years
    The command to install xocde tools is xcode-select --install. Also, homebrew/dupes is deprecated. Can just use brew install tcl-tk.
  • fralau
    fralau about 6 years
    If you want this to work, you need to apply the advice by JBallin: brew install python --with-tcl-tk
  • JBallin
    JBallin about 6 years
    @fralau I'm not sure how I left that out! Updated my answer, thanks!
  • fralau
    fralau about 6 years
    You're very welcome. If we were not allowed to leave anything out, we would never contribute in the first place!
  • Josh
    Josh almost 6 years
    This worked with LinuxBrew too! (minus the xcode-select step)
  • pjs
    pjs over 5 years
    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!.
  • Garini
    Garini over 5 years
    This is not working anymore as the optional --with-tcl-tk disappeared.
  • lab
    lab about 5 years
    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.
  • JBallin
    JBallin about 5 years
    @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?
  • rain01
    rain01 about 5 years
    I'm not getting any warning, I just get an error now: Error: invalid option: --with-tcl-tk
  • MRX
    MRX over 4 years
    --with-tcl-tk is not working as mentioned by @pjs
  • Daniel Gonzalez
    Daniel Gonzalez about 4 years
    --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.
  • Ken Shirriff
    Ken Shirriff about 4 years
    After trying many other solutions, the script above fixed the problem for me on macOS Catalina with python3. Now I can use matplotlib.
  • flow2k
    flow2k about 3 years
  • juanfal
    juanfal about 3 years
    Thanks a lot… getting crazy with this. No info at all anywhere. It did the magic brew install python-tk
  • Vikas Pandey
    Vikas Pandey almost 3 years
    brew install python-tk this one worked. thanks @sgon00
  • PeterK
    PeterK almost 3 years
    Great, for python 3.9. The recipe is brew install [email protected]. Any idea how to do this for python 3.8?
  • Near Future
    Near Future almost 3 years
    thanks, that one helped here, how should i search such info for the future from official docs ? ("brew tkinter" pointed me here)