为什么我的Python通过家庭酿造安装不包括Tkinter

时间:2016-04-21 05:59:07

标签: python macos tkinter

我在Mac上通过自制软件安装了我的Python。

brew install python

之后我将我的python版本检查为2.7.11,然后我尝试执行

import Tkinter

我收到以下错误消息:

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

9 个答案:

答案 0 :(得分:24)

我正在运行 MacOS Big Sur (11.2.3)。

使用 python2,我内置了 Tkinter

使用python3,需要手动安装,非常简单,运行即可:

$ brew install python-tk

要在终端中运行 python2,请执行 python file.py

要在终端中运行 python3,请执行 python3 file.py

答案 1 :(得分:18)

基于上面的评论以及Python必须链接到Tcl / Tk框架的事实:

如果您没有Xcode命令行工具,请安装:

xcode-select --install

如果您没有安装Tcl / Tk brew(请查看 brew list ),请安装:

brew install tcl-tk

然后,运行&#34; brew uninstall python&#34;如果没有安装选项 - with-tcl-tk (当前官方选项)。然后再次安装Python,将其链接到brew安装的Tcl / Tk:

brew install python --with-tcl-tk

答案 2 :(得分:8)

12/18更新:No longer possible various reasons

现在已经过时了。如果要删除这些警告,您必须直接从python.org安装Python。

2018年更新

brew reinstall python --with-tcl-tk

注意:Homebrew现在默认使用Python 3 - Homebrew BlogDocs

测试

python应该提出系统的Python 2,python3应该调出Python 3。

idle指向系统Python / tcl-tk。它将显示过时的tcl-tk错误(除非您brew install python@2 --with-tcl-tk

idle3应该在没有警告的情况下调出Python 3.

买者

--with-tcl-tk将直接从python.org安装python,您在运行brew info python时会看到这一点。

更多信息here

答案 3 :(得分:7)

如果您使用的是pyenv,则可以尝试通过自制程序安装tcl-tk,然后激活环境。 vars。 this answer中有详细说明,请参见其注意事项部分。激活那些环境。 vars。在通过自制软件安装python之前,可能对您有用:

※ 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"
※ brew reinstall python

答案 4 :(得分:7)

对于 brewpython3,您必须单独安装 Tinker。

安装python时的brew消息:

<块引用>

tkinter 不再包含在此公式中,但可以单独使用:

brew install python-tk@3.9

答案 5 :(得分:4)

在mac OSX上,您必须单独安装TCL:

您可以在此处找到说明和下载内容:https://www.tcl.tk/software/tcltk/并在那里:http://wiki.tcl.tk/1013

这需要一点点努力,但既不复杂也不困难。

答案 6 :(得分:3)

可能是因为您没有最新的Xcode命令行工具,因此从源代码而不是从瓶子中生成python。尝试:

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

答案 7 :(得分:3)

现在有点复杂了,确实,您仍然需要使用xcode命令行工具和Homebrew作为开始。但是程序不断变化。 Homebrew早就取消了对tcl-tk的支持,而Apple仍仅提供v8.5的tcl-tk。无论如何,有可能,我亲自维护了github要点来解决这些问题。

最新更新使用的是python 3.8.1(稍后也可能会在3.8.x分支上使用),请参见此处,只需按照概述的步骤进行即可。 github gist link to install tcl-tk with python

答案 8 :(得分:2)

在 MacOS 11.13.1 上使用

brew install python
brew install python-tk

我现在可以在 matplotlib 中选择 TkAgg,但是当我在 ipython 中使用它时,我收到一条错误消息

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

结果

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