本文介绍了无法在安装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 和 上使用pyenv管理Python版本.
- (您要IDLE-Python的开发环境-在您的macOS上工作
- 或者您希望tkinter模块工作)
- 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 on import 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 run brew install tcl-tk
- in shell run echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
- reload shell by quitting Terminal app or run source ~/.zshrc
- after reloaded check that tck-tk is in $PATH. Run echo $PATH | grep --color=auto tcl-tk. As the result you should see your $PATH contents with tcl-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"
测试
- 在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 run python -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 to pyenv official readme from GitHub)
- Python 3.8.x - 3.9.x (installed with pyenv install <version> command)
这篇关于无法在安装pyenv的情况下运行IDLE:`可能未为Tk配置Python``ModuleNotFoundError:没有名为_tkinter的模块'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!