简体   繁体   English

为什么通过 Homebrew 安装的 Python 不包括 Tkinter

[英]Why does Python installed via Homebrew not include Tkinter

I've installed Python via Homebrew on my Mac.我在我的 Mac 上通过 Homebrew 安装了 Python。

brew install python

After that I checked my Python version as 2.7.11, then I tried to perform之后我检查了我的 Python 版本为 2.7.11,然后我尝试执行

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

I am running MacOS Big Sur (11.2.3).我正在运行 MacOS Big Sur (11.2.3)。

With python2, I have Tkinter built-in.使用 python2,我内置了Tkinter

With python3, it has to be installed manually and it's very simple, just run:使用python3,它必须手动安装,非常简单,只需运行:

$ brew install python-tk

To run python2 in a terminal, execute python file.py .要在终端中运行 python2,请执行python file.py

To run python3 in a terminal, execute python3 file.py .要在终端中运行 python3,请执行python3 file.py

Based on the comments from above and the fact that Python must be linked to Tcl/Tk framework:基于上面的评论以及 Python 必须链接到 Tcl/Tk 框架的事实:

If you don't have Xcode command line tools, install those:如果您没有 Xcode 命令行工具,请安装这些:

xcode-select --install

If you don't have Tcl/Tk brew installation (check brew list ), install that:如果您没有 Tcl/Tk brew 安装(检查brew list ),请安装:

brew install tcl-tk

Then, run "brew uninstall python" if that was not installed with option --with-tcl-tk (the current official option).然后,如果未使用选项--with-tcl-tk (当前官方选项)安装,请运行“brew uninstall python”。 Then install Python again, linking it to the brew installed Tcl/Tk:然后再次安装 Python,将其链接到 brew 安装的 Tcl/Tk:

brew install python --with-tcl-tk

12/18 Update: No longer possible for various reasons . 12/18 更新:由于各种原因不再可能

Below is now outdated.下面现在已经过时了。 You'll have to install Python directly from python.org if you want to remove those warnings.如果要删除这些警告,则必须直接从python.org安装 Python。


2018 Update 2018 更新

brew reinstall python --with-tcl-tk

Note: Homebrew now uses Python 3 by default - Homebrew Blog .注意:Homebrew 现在默认使用 Python 3 - Homebrew 博客 Docs .文档


Testing测试

python should bring up system's Python 2, python3 should bring up Python 3. python应该调出系统的 Python 2, python3应该调出 Python 3。

idle points to system Python/tcl-tk. idle指向系统 Python/tcl-tk。 It will show an out-dated tcl-tk error (unless you brew install python@2 --with-tcl-tk )它将显示过时的 tcl-tk 错误(除非您brew install python@2 --with-tcl-tk

idle3 should bring up Python 3 with no warnings. idle3应该在没有警告的情况下启动 Python 3。

Caveat警告

--with-tcl-tk will install python directly from python.org , which you'll see when you run brew info python . --with-tcl-tk将直接从python.org安装python ,当你运行brew info python时你会看到。

More info here .更多信息在这里

If you're using pyenv you can try installing tcl-tk via homebrew and then activating the env.如果您使用的是pyenv您可以尝试通过自制软件安装tcl-tk ,然后激活 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:在通过自制软件安装 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

With brew and python3 you have to install Tinker separately.使用brewpython3,您必须单独安装 Tinker。

brew message while installing python:安装python时的brew消息:

tkinter is no longer included with this formula, but it is available separately: tkinter 不再包含在此公式中,但可以单独使用:

brew install python-tk@3.9

On mac OSX you must install TCL separately:在 mac OSX 上,您必须单独安装 TCL:

You will find instructions and dowloadables here: https://www.tcl.tk/software/tcltk/ and there: http://wiki.tcl.tk/1013您可以在这里找到说明和可下载文件: https ://www.tcl.tk/software/tcltk/ 和那里: http ://wiki.tcl.tk/1013

It requires a little bit of effort, but it is neither complicated nor difficult.它需要一点点努力,但它既不复杂也不困难。

It may be because you don't have the latest Xcode command line tools so brew built python from source instead of from bottle.可能是因为您没有最新的 Xcode 命令行工具,所以 brew 从源代码而不是从 Bottle 构建 python。 Try:尝试:

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

It is a bit more complicated now, true you still need to have xcode command line tools and homebrew as a start.现在有点复杂,确实您仍然需要使用 xcode 命令行工具和自制软件作为开始。 But the procedure changes constantly.但程序不断变化。 Homebrew took out tcl-tk support long ago, and apple still only supplies v8.5 of tcl-tk. Homebrew 很早就去掉了 tcl-tk 的支持,苹果还只提供了 v8.5 的 tcl-tk。 Anyway, it is possible, and I maintain a github gist personally to fix these issues.无论如何,这是可能的,我个人维护了一个 github gist 来解决这些问题。

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

On MacOS 11.13.1 using在 MacOS 11.13.1 上使用

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我现在可以在 matplotlib 中选择TkAgg ,但是当我在 ipython 中使用它时,我收到一条错误消息

%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

It doesn't work in any OS whatsoever that doesn't have the TCL Toolkit already installed.它不适用于任何未安装 TCL 工具包的操作系统。 While it's either already installed in many Linux distributions and/or bundled with Python bundles downloaded from python.org for Windows and Linux - and aa consequence of that is generally wrongly assumed that it's part of Python - it's not the case for macOS.虽然它已经安装在许多 Linux 发行版中和/或与从 python.org 下载的Python 捆绑包捆绑在一起,用于 Windows 和 Linux - 其结果通常被错误地认为它是 macOS 的一部分,而不是 8827548599 There's official reasons for this described in the appropriate document :相应的文档中描述了官方原因:

If you are using macOS 12 Monterey or later, you may see problems with file open and save dialogs when using IDLE or other tkinter-based applications.如果您使用的是 macOS 12 Monterey 或更高版本,您可能会在使用 IDLE 或其他基于 tkinter 的应用程序时看到文件打开和保存对话框的问题。 The most recent versions of python.org installers (for 3.10.0 and 3.9.8) have patched versions of Tk to avoid these problems.最新版本的 python.org 安装程序(适用于 3.10.0 和 3.9.8)已对 Tk 版本进行了修补以避免这些问题。 They should be fixed in an upcoming Tk 8.6.12 release.它们应该在即将发布的 Tk 8.6.12 版本中得到修复。

If you are using a Python from any current python.org Python installer for macOS (3.10.0+ or 3.9.0+), no further action is needed to use IDLE or tkinter. A built-in version of Tcl/Tk 8.6 will be used.如果您正在使用来自任何当前 python.org Python macOS(3.10.0+ 或 3.9.0+)安装程序的 Python,则无需进一步操作即可使用 IDLE 或 tkinter。Tcl/Tk 8.6 的内置版本将使用。

If you are using macOS 10.6 or later, the Apple-supplied Tcl/Tk 8.5 has serious bugs that can cause application crashes.如果您使用的是 macOS 10.6 或更高版本,Apple 提供的 Tcl/Tk 8.5 存在严重错误,可能导致应用程序崩溃。 If you wish to use IDLE or Tkinter, do not use the Apple-supplied Pythons.如果您想使用 IDLE 或 Tkinter,请不要使用 Apple 提供的 Python。 Instead, install and use a newer version of Python from python.org or a third-party distributor that supplies or links with a newer version of Tcl/Tk.相反,安装并使用来自 python.org 的更新版本的 Python 或提供或链接更新版本的 Tcl/Tk 的第三方分销商。

Python's integrated development environment, IDLE, and the tkinter GUI toolkit it uses, depend on the Tk GUI toolkit which is not part of Python itself. Python 的集成开发环境 IDLE 及其使用的 tkinter GUI 工具包依赖于 Tk GUI 工具包,它不是 Python 本身的一部分。 For best results, it is important that the proper release of Tcl/Tk is installed on your machine.为获得最佳结果,在您的机器上安装正确版本的 Tcl/Tk 非常重要。 For recent Python installers for macOS downloadable from this website, here is a summary of current recommendations followed by more detailed information.对于可从本网站下载的最新 Python macOS 安装程序,这里是当前建议的摘要,后面是更详细的信息。


It has been already mentioned but the most popular way to do it is:已经提到过,但最流行的方法是:

$ brew install python-tk 

It will work because python-tk formulae depends on other two: python and tcl-tk (hence you don't need to additionally do brew install python ).它会起作用,因为python-tk公式取决于其他两个: pythontcl-tk (因此您不需要另外执行brew install python )。

If you had already installed python with homebrew如果你已经用自制软件安装了 python

$ brew install python  

You can have tkinter with你可以有 tkinter

$ brew install tcl-tk  

This is what worked for me with macOS Monterey:这对我使用 macOS Monterey 有用:

brew install python-tk@3.10 or brew install python-tk@3.9 brew install python-tk@3.10brew install python-tk@3.9

Depending on which Python version you're running.取决于您正在运行的 Python 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM