【问题标题】:Why does Python installed via Homebrew not include Tkinter为什么通过 Homebrew 安装的 Python 不包括 Tkinter
【发布时间】:2016-08-14 03:18:07
【问题描述】:

我已经通过 Homebrew 在我的 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

【问题讨论】:

    标签: python python-3.x macos tkinter


    【解决方案1】:

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

    使用 python2,我有 Tkinter 内置。

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

    $ brew install python-tk
    

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

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

    【讨论】:

    • 非常感谢……对此感到疯狂。任何地方都没有任何信息。它做了魔术brew install python-tk
    • brew install python-tk 这个有效。谢谢@sgon00
    • 很好,适用于 python 3.9。配方是brew install python-tk@3.9。知道如何为 python 3.8 执行此操作吗?
    • 谢谢,这里有帮助,我应该如何从官方文档中搜索这些信息以备将来使用? (“brew tkinter”指向我这里)
    【解决方案2】:

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

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

    xcode-select --install
    

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

    brew install tcl-tk
    

    然后,如果未使用选项 --with-tcl-tk(当前官方选项)安装,则运行“brew uninstall python”。然后再次安装 Python,将其链接到 brew 安装的 Tcl/Tk:

    brew install python --with-tcl-tk
    

    【讨论】:

    • 安装xocde工具的命令是xcode-select --install。此外,homebrew/dupes 已被弃用。可以使用brew install tcl-tk
    • 这也适用于 LinuxBrew! (减去 xcode-select 步骤)
    • 这似乎不再起作用。我收到了Warning: python: this formula has no --with-tcl-tk option so it will be ignored! 的消息。
    • 我没有收到任何警告,我现在只是收到一个错误:Error: invalid option: --with-tcl-tk
    • --with-tcl-tk 不幸被删除,你可以看到here。对于我的特定系统,运行brew uninstall --ignore-dependencies python &amp;&amp; brew install python 就可以了。现在还不清楚为什么会这样,但它确实有效。我想这与重建和链接东西以及其他魔法有关。
    【解决方案3】:

    更新:其他答案已找到解决方法,因此此答案现已过时。

    12/18 更新:No longerpossiblevarious 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

    【讨论】:

    • 如果你想让这个工作,你需要应用 JBallin 的建议:brew install python --with-tcl-tk
    • @fralau 我不知道我是怎么漏掉的!更新我的答案,谢谢!
    • 不客气。如果不允许我们遗漏任何内容,我们一开始就不会做出贡献!
    • 这不再起作用,因为可选的--with-tcl-tk 消失了。
    • 请注意,带有 tkinter 的版本现在是默认版本:discourse.brew.sh/t/python3-installation-with-tkinter/3636/8 可能需要强制重新安装才能获得它。
    【解决方案4】:

    使用 brewpython3 您必须单独安装 Tinker。

    安装 python 时的 brew 消息:

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

    brew install python-tk@3.9
    

    【讨论】:

    【解决方案5】:

    如果您使用pyenv,您可以尝试通过自制软件安装tcl-tk,然后激活环境。瓦尔斯。在其警告部分中提到,详见this answer。激活那些环境。瓦尔斯。在通过自制软件安装 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"
    ※ 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
    

    【讨论】:

      【解决方案6】:

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

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

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

      【讨论】:

      • 谢谢,我已经安装了 ActiveTcl 或者自己构建 Tcl/Tk,但仍然出现同样的错误。
      • 我做了同样的事情并重新安装了 Python,并且有同样的错误。我猜有办法让brew install 使用不同的TCL,但我不知道。有--with-brewed-tk,但是有--with-activetcl-at-path ________什么的吗?
      • 对不起,我帮不了你,IDK。
      【解决方案7】:

      这可能是因为你没有最新的 Xcode 命令行工具,所以 brew 从源代码而不是从瓶子构建 python。试试:

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

      【讨论】:

      • 你需要使用--with-tcl-tk而不是--use-brewed-tk
      • --with-tcl-tk 不像@pjs 提到的那样工作
      【解决方案8】:

      现在有点复杂了,你仍然需要 xcode 命令行工具和 homebrew 作为开始。但程序不断变化。 Homebrew 很久以前就退出了 tcl-tk 支持,而苹果仍然只提供 v8.5 的 tcl-tk。无论如何,这是可能的,我个人维护了一个 github gist 来解决这些问题。

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

      【讨论】:

      • 在尝试了许多其他解决方案之后,上面的脚本在 macOS Catalina 上使用 python3 解决了我的问题。现在我可以使用 matplotlib。
      【解决方案9】:

      在 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
      

      【讨论】:

        猜你喜欢
        • 2012-10-11
        • 1970-01-01
        • 2015-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        相关资源
        最近更新 更多