Why does Python installed via Homebrew not include Tkinter

PythonPython 3.xMacosTkinter

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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 - Python

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 - Python

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 - Python

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

Solution 5 - Python

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

Solution 6 - Python

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.

Solution 7 - Python

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

Solution 8 - Python

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

Solution 9 - Python

On MacOS 11.13.1 using

brew install python
brew install python-tk

I can now select TkAgg in matplotlib, but when I use it in ipython I get an error message

%pylab
matplotlib.use('tkagg')
plot([0,1])

results in

2021-05-07 21:51:02.954 Python[10773:71016] -[NSApplication macOSVersion]: unrecognized selector sent to instance 0x11779f8c0
2021-05-07 21:51:02.956 Python[10773:71016] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication macOSVersion]: unrecognized selector sent to instance 0x11779f8c0'
*** First throw call stack:
(
        0   CoreFoundation                      0x00000001a0d97db8 __exceptionPreprocess + 240
        1   libobjc.A.dylib                     0x00000001a0ac10a8 objc_exception_throw + 60
        2   CoreFoundation                      0x00000001a0e28ba0 -[NSObject(NSObject) __retain_OA] + 0
        3   CoreFoundation                      0x00000001a0cf91e4 ___forwarding___ + 1444
        4   CoreFoundation                      0x00000001a0cf8b80 _CF_forwarding_prep_0 + 96
        5   libtk8.6.dylib                      0x000000012754a844 GetRGBA + 308
        6   libtk8.6.dylib                      0x000000012754a208 SetCGColorComponents + 132
        7   libtk8.6.dylib                      0x000000012754a65c TkpGetColor + 572
        8   libtk8.6.dylib                      0x00000001274ac714 Tk_GetColor + 220
        9   libtk8.6.dylib                      0x000000012749fea0 Tk_Get3DBorder + 204
        10  libtk8.6.dylib                      0x000000012749fcac Tk_Alloc3DBorderFromObj + 144
        11  libtk8.6.dylib                      0x00000001274adadc DoObjConfig + 840
        12  libtk8.6.dylib                      0x00000001274ad690 Tk_InitOptions + 348
        13  libtk8.6.dylib                      0x00000001274ad58c Tk_InitOptions + 88
        14  libtk8.6.dylib                      0x00000001274d4cb4 CreateFrame + 1448
        15  libtk8.6.dylib                      0x00000001274d4fac TkListCreateFrame + 156
        16  libtk8.6.dylib                      0x00000001274cde80 Initialize + 1848
        17  _tkinter.cpython-39-darwin.so       0x000000012059a31c Tcl_AppInit + 80
        18  _tkinter.cpython-39-darwin.so       0x000000012059487c Tkapp_New + 592
        19  _tkinter.cpython-39-darwin.so       0x000000012059410c _tkinter_create + 580
        20  Python                              0x00000001007150c4 cfunction_vectorcall_FASTCALL + 88
        21  Python                              0x00000001007bac4c call_function + 128
        22  Python                              0x00000001007b8640 _PyEval_EvalFrameDefault + 39844
        23  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        24  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        25  Python                              0x00000001006c58ac _PyObject_FastCallDictTstate + 208
        26  Python                              0x0000000100739bf4 slot_tp_init + 188
        27  Python                              0x000000010073f850 type_call + 300
        28  Python                              0x00000001006c5590 _PyObject_MakeTpCall + 132
        29  Python                              0x00000001007bacd8 call_function + 268
        30  Python                              0x00000001007b86e8 _PyEval_EvalFrameDefault + 40012
        31  Python                              0x00000001006c61fc _PyFunction_Vectorcall + 180
        32  Python                              0x00000001006c8c98 method_vectorcall + 124
        33  Python                              0x00000001007bac4c call_function + 128
        34  Python                              0x00000001007b8640 _PyEval_EvalFrameDefault + 39844
        35  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        36  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        37  Python                              0x00000001006c8c98 method_vectorcall + 124
        38  Python                              0x00000001006c5e40 PyVectorcall_Call + 184
        39  Python                              0x00000001007b880c _PyEval_EvalFrameDefault + 40304
        40  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        41  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        42  Python                              0x00000001006c5e40 PyVectorcall_Call + 184
        43  Python                              0x00000001007b880c _PyEval_EvalFrameDefault + 40304
        44  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        45  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        46  Python                              0x00000001007bac4c call_function + 128
        47  Python                              0x00000001007b8664 _PyEval_EvalFrameDefault + 39880
        48  Python                              0x00000001006c61fc _PyFunction_Vectorcall + 180
        49  Python                              0x00000001007bac4c call_function + 128
        50  Python                              0x00000001007b8664 _PyEval_EvalFrameDefault + 39880
        51  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        52  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        53  Python                              0x00000001007bac4c call_function + 128
        54  Python                              0x00000001007b8664 _PyEval_EvalFrameDefault + 39880
        55  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        56  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        57  Python                              0x00000001007bac4c call_function + 128
        58  Python                              0x00000001007b8664 _PyEval_EvalFrameDefault + 39880
        59  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        60  Python                              0x00000001007a86a0 builtin_exec + 356
        61  Python                              0x00000001007150c4 cfunction_vectorcall_FASTCALL + 88
        62  Python                              0x00000001007bac4c call_function + 128
        63  Python                              0x00000001007b8664 _PyEval_EvalFrameDefault + 39880
        64  Python                              0x00000001006da678 gen_send_ex + 192
        65  Python                              0x00000001007b35b4 _PyEval_EvalFrameDefault + 19224
        66  Python                              0x00000001006da678 gen_send_ex + 192
        67  Python                              0x00000001007b35b4 _PyEval_EvalFrameDefault + 19224
        68  Python                              0x00000001006da678 gen_send_ex + 192
        69  Python                              0x00000001006d1cb0 method_vectorcall_O + 108
        70  Python                              0x00000001007bac4c call_function + 128
        71  Python                              0x00000001007b85c4 _PyEval_EvalFrameDefault + 39720
        72  Python                              0x00000001006c61fc _PyFunction_Vectorcall + 180
        73  Python                              0x00000001007bac4c call_function + 128
        74  Python                              0x00000001007b8664 _PyEval_EvalFrameDefault + 39880
        75  Python                              0x00000001006c61fc _PyFunction_Vectorcall + 180
        76  Python                              0x00000001007bac4c call_function + 128
        77  Python                              0x00000001007b85c4 _PyEval_EvalFrameDefault + 39720
        78  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        79  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        80  Python                              0x00000001006c8c98 method_vectorcall + 124
        81  Python                              0x00000001007bac4c call_function + 128
        82  Python                              0x00000001007b86e8 _PyEval_EvalFrameDefault + 40012
        83  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        84  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        85  Python                              0x00000001007bac4c call_function + 128
        86  Python                              0x00000001007b85c4 _PyEval_EvalFrameDefault + 39720
        87  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        88  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        89  Python                              0x00000001007bac4c call_function + 128
        90  Python                              0x00000001007b85c4 _PyEval_EvalFrameDefault + 39720
        91  Python                              0x00000001006c61fc _PyFunction_Vectorcall + 180
        92  Python                              0x00000001007bac4c call_function + 128
        93  Python                              0x00000001007b85c4 _PyEval_EvalFrameDefault + 39720
        94  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        95  Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        96  Python                              0x00000001006c8c98 method_vectorcall + 124
        97  Python                              0x00000001006c5e40 PyVectorcall_Call + 184
        98  Python                              0x00000001007b880c _PyEval_EvalFrameDefault + 40304
        99  Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        100 Python                              0x00000001006c62b4 _PyFunction_Vectorcall + 364
        101 Python                              0x00000001007bac4c call_function + 128
        102 Python                              0x00000001007b8664 _PyEval_EvalFrameDefault + 39880
        103 Python                              0x00000001007ada9c _PyEval_EvalCode + 444
        104 Python                              0x0000000100805498 run_eval_code_obj + 136
        105 Python                              0x00000001008053ac run_mod + 112
        106 Python                              0x0000000100802be8 pyrun_file + 168
        107 Python                              0x000000010080250c pyrun_simple_file + 276
        108 Python                              0x00000001008023b8 PyRun_SimpleFileExFlags + 80
        109 Python                              0x0000000100822560 pymain_run_file + 320
        110 Python                              0x0000000100821b2c pymain_run_python + 412
        111 Python                              0x000000010082194c Py_RunMain + 24
        112 Python                              0x0000000100822f50 pymain_main + 36
        113 Python                              0x00000001008231c8 Py_BytesMain + 40
        114 libdyld.dylib                       0x00000001a0c38420 start + 4
)
libc++abi: terminating with uncaught exception of type NSException
Abort trap: 6

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel ChenView Question on Stackoverflow
Solution 1 - Pythonsgon00View Answer on Stackoverflow
Solution 2 - PythonjalairoView Answer on Stackoverflow
Solution 3 - PythonJBallinView Answer on Stackoverflow
Solution 4 - PythonCarl GView Answer on Stackoverflow
Solution 5 - Pythonnash0rnView Answer on Stackoverflow
Solution 6 - PythonReblochon MasqueView Answer on Stackoverflow
Solution 7 - PythonshiView Answer on Stackoverflow
Solution 8 - PythonCsaba K.View Answer on Stackoverflow
Solution 9 - Pythonuser3622450View Answer on Stackoverflow