53

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
  • 3,720
  • 2
  • 19
  • 38
Daniel Chen
  • 1,783
  • 5
  • 23
  • 33

9 Answers9

57

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,177
  • 1
  • 32
  • 49
24

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
  • 6,568
  • 1
  • 40
  • 45
jalairo
  • 365
  • 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
  • 13
    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
  • `--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
11

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
  • 6,568
  • 1
  • 40
  • 45
  • 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

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
  • 161
  • 1
  • 3
8

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,134
  • 2
  • 40
  • 66
Carl G
  • 15,695
  • 12
  • 82
  • 107
6

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
  • 33,202
  • 9
  • 48
  • 71
  • 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
4

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
  • 288
  • 1
  • 9
3

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.
  • 109
  • 6
  • 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
3

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
user3622450
  • 129
  • 1
  • 2