为什么我通过 Home brew 安装的 Python 不包括 Tkinter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36760839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why my Python installed via home brew not include Tkinter
提问by Daniel Chen
I've installed my Python via homebrew on Mac.
我已经在 Mac 上通过自制软件安装了我的 Python。
brew install python
and after that I've checked my python version as 2.7.11, then I've tried perform
之后我检查了我的python版本为2.7.11,然后我尝试执行
import Tkinter
I've 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
回答by jalairo
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). Then install Python again, linking it to the brew installed Tcl/Tk:
然后,如果未使用选项--with-tcl-tk(当前官方选项)安装,请运行“brew uninstall python” 。然后再次安装 Python,将其链接到 brew 安装的 Tcl/Tk:
brew install python --with-tcl-tk
回答by JBallin
12/18 Update: No longerpossiblefor various reasons.
12/18 更新:由于各种原因不再可能。
Below is now outdated. You'll have to install Python directly from python.orgif 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. Docs.
注意:Homebrew 现在默认使用 Python 3 - Homebrew 博客。文档。
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. It will show an out-dated tcl-tk error (unless you brew install python@2 --with-tcl-tk
)
idle
指向系统 Python/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
直接从python.org安装,运行brew info python
.
More info here.
更多信息在这里。
回答by Reblochon Masque
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.
它需要一点点努力,但它既不复杂也不困难。
回答by shi
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 命令行工具,所以 brew 从源代码而不是从 Bottle 构建 python。尝试:
xcode-select --install
brew uninstall python
brew install python --use-brewed-tk
回答by Carl G
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:
如果您正在使用,pyenv
您可以尝试tcl-tk
通过自制软件安装,然后激活 env。变量。在其警告部分中提到,如本答案中所述。激活那些环境。变量。在通过自制软件安装 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
回答by Csaba K.
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.
现在有点复杂,确实您仍然需要使用 xcode 命令行工具和自制软件作为开始。但程序不断变化。Homebrew 很早就去掉了 tcl-tk 的支持,苹果还只提供了 v8.5 的 tcl-tk。无论如何,这是可能的,我个人维护了一个 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. github gist link to install tcl-tk with python
最新更新是使用 python 3.8.1(稍后也可能在 3.8.x 分支上使用)请参见此处,只需按照概述的步骤操作即可。 使用 python 安装 tcl-tk 的 github gist 链接