无法在安装pyenv的情况下运行IDLE:`可能未为Tk配置Python``ModuleNotFoundError:没有名为_tkinter的模块' [英] Can't run IDLE with pyenv installation: `Python may not be configured for Tk` `ModuleNotFoundError: No module named _tkinter'
本文介绍了无法在安装pyenv的情况下运行IDLE:`可能未为Tk配置Python``ModuleNotFoundError:没有名为_tkinter的模块'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我最近花了几个小时来使tkinter
和IDLE在我的pyenv
Python安装(macOS)上正常工作.
I recently spent couple hours making tkinter
and IDLE work on my pyenv
Python installation (macOS).
你为什么在这里?
- 您可以在macOS 和 上使用
- (您要IDLE-Python的开发环境-在您的macOS上工作
- 或者您希望
tkinter
模块工作)
pyenv
管理Python版本.
- You manage Python versions with
pyenv
on macOS and - ( You want IDLE - the development environment for Python - work on your macOS
- or you want
tkinter
module work )
怎么了?
您收到以下错误之一:
-
Python may not be configured for Tk
-
import _tkinter # If this fails your Python may not be configured for Tk
-
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
-
ModuleNotFoundError: No module named '_tkinter'
Python may not be configured for Tk
onimport tkinter
import _tkinter # If this fails your Python may not be configured for Tk
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
ModuleNotFoundError: No module named '_tkinter'
推荐答案
以下是逐步指南,以使IDLE和tkinter
正常工作:
Here is step by step guide to make IDLE and tkinter
work:
- 使用Homebrew安装
tcl-tk
.在shell中运行brew install tcl-tk
- 在shell中运行
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
- 通过退出
Terminal
应用程序或运行source ~/.zshrc
重新加载Shell
重新加载 - 后,请检查
$PATH
中是否包含tck-tk
.运行echo $PATH | grep --color=auto tcl-tk
.结果,您应该看到$ PATH内容带有突出显示的tcl-tk
- 现在我们从步骤1的Homebrew输出中运行三个命令
- install
tcl-tk
with Homebrew. In shell runbrew install tcl-tk
- in shell run
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
- reload shell by quitting
Terminal
app or runsource ~/.zshrc
- after reloaded check that
tck-tk
is in$PATH
. Runecho $PATH | grep --color=auto tcl-tk
. As the result you should see your $PATH contents withtcl-tk
highlighted - now we run three commands from Homebrew's output from step #1
- 在shell中运行
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
- 在shell中运行
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
- 在shell中运行
export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
- in shell run
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
- in shell run
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
- in shell run
export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
pyenv
安装了Python版本,请使用pyenv uninstall <your python version>
卸载它.例如. pyenv uninstall 3.8.2
python-build
使用的环境变量.在shell中运行PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'"
注意:将来使用与Homebrew一起实际安装的tck-tk
版本.在发布时,8.6
是实际的 pyenv
和pyenv install <version>
一起安装Python.例如. pyenv install 3.8.2
pyenv
then uninstall it with pyenv uninstall <your python version>
. E.g. pyenv uninstall 3.8.2
python-build
. In shell run PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'"
Note: in future use tck-tk
version that actually installed with Homebrew. At the moment of posting 8.6
was the actualpyenv
with pyenv install <version>
. E.g. pyenv install 3.8.2
测试
- 在shell中运行
pyenv global <verion that you've just installed>
- 现在检查IDLE.在外壳程序中运行
idle
.您应该看到没有任何警告的"IDLE"窗口和以红色打印的文本".
- in shell run
pyenv global <verion that you've just installed>
- now check IDLE. In shell run
idle
. You should see IDLE window without any warnings and "text printed in red".
- 现在检查
tkinter
.在外壳程序中运行python -m tkinter -c "tkinter._test()"
.您应该看到如图所示的测试窗口:
- now check
tkinter
. In shell runpython -m tkinter -c "tkinter._test()"
. You should see test window like on the image:
就是这样!
我的环境:
检查执行上述步骤是否出错:
check this is something went wrong executing steps above:
- macOS Catalina
-
zsh
(包含在macOS Catalina中)= 上面的"shell" - Homebrew(已安装Homebrew官方网站上的说明)
-
pyenv
(已根据GitHub上的pyenv
官方自述文件,安装了Homebrew和PATH) - Python
3.8.x
-3.9.x
(通过pyenv install <version>
命令安装)
- macOS Catalina
zsh
(included in macOS Catalina) = "shell" above- Homebrew (installed with instructions from Homebrew official website)
pyenv
(installed with Homebrew and PATH updated according topyenv
official readme from GitHub)- Python
3.8.x
-3.9.x
(installed withpyenv install <version>
command)
这篇关于无法在安装pyenv的情况下运行IDLE:`可能未为Tk配置Python``ModuleNotFoundError:没有名为_tkinter的模块'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文