无法在安装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'

查看:371
本文介绍了无法在安装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).

你为什么在这里?

  1. 您可以在macOS
  2. 上使用pyenv管理Python版本.
  3. (您要IDLE-Python的开发环境-在您的macOS上工作
  4. 或者您希望tkinter模块工作)
  1. You manage Python versions with pyenv on macOS and
  2. ( You want IDLE - the development environment for Python - work on your macOS
  3. or you want tkinter module work )

怎么了?

您收到以下错误之一:

    >上的
  1. Python may not be configured for Tk
  2. import _tkinter # If this fails your Python may not be configured for Tk
  3. RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
  4. ModuleNotFoundError: No module named '_tkinter'
  1. Python may not be configured for Tk on import tkinter
  2. import _tkinter # If this fails your Python may not be configured for Tk
  3. RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
  4. ModuleNotFoundError: No module named '_tkinter'

推荐答案

以下是逐步指南,以使IDLE和tkinter正常工作:

Here is step by step guide to make IDLE and tkinter work:

  1. 使用Homebrew安装tcl-tk.在shell中运行brew install tcl-tk
  2. 在shell中运行echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
  3. 通过退出Terminal应用程序或运行source ~/.zshrc
  4. 重新加载Shell 重新加载
  5. 后,请检查$PATH中是否包含tck-tk.运行echo $PATH | grep --color=auto tcl-tk.结果,您应该看到$ PATH内容带有突出显示的tcl-tk
  6. 现在我们从步骤1的Homebrew输出中运行三个命令
  1. install tcl-tk with Homebrew. In shell run brew install tcl-tk
  2. in shell run echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
  3. reload shell by quitting Terminal app or run source ~/.zshrc
  4. 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
  5. now we run three commands from Homebrew's output from step #1
  1. 在shell中运行export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
  2. 在shell中运行export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
  3. 在shell中运行export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
  1. in shell run export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
  2. in shell run export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
  3. 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是实际的
  • 最后将pyenvpyenv install <version>一起安装Python.例如. pyenv install 3.8.2
  • if you have your Python version already installed with pyenv then uninstall it with pyenv uninstall <your python version>. E.g. pyenv uninstall 3.8.2
  • set environment variable that will be used by 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 actual
  • finally install Python with pyenv with pyenv install <version>. E.g. pyenv install 3.8.2
  • 测试

    1. 在shell中运行pyenv global <verion that you've just installed>
    2. 现在检查IDLE.在外壳程序中运行idle.您应该看到没有任何警告的"IDLE"窗口和以红色打印的文本".
    1. in shell run pyenv global <verion that you've just installed>
    2. now check IDLE. In shell run idle. You should see IDLE window without any warnings and "text printed in red".

    1. 现在检查tkinter.在外壳程序中运行python -m tkinter -c "tkinter._test()".您应该看到如图所示的测试窗口:
    1. 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:

    1. macOS Catalina
    2. zsh(包含在macOS Catalina中)= 上面的"shell"
    3. Homebrew(已安装Homebrew官方网站上的说明)
    4. pyenv(已根据GitHub上的pyenv官方自述文件,安装了Homebrew和PATH)
    5. Python 3.8.x-3.9.x(通过pyenv install <version>命令安装)
    1. macOS Catalina
    2. zsh (included in macOS Catalina) = "shell" above
    3. Homebrew (installed with instructions from Homebrew official website)
    4. pyenv (installed with Homebrew and PATH updated according to pyenv official readme from GitHub)
    5. Python 3.8.x - 3.9.x (installed with pyenv install <version> command)

    这篇关于无法在安装pyenv的情况下运行IDLE:`可能未为Tk配置Python``ModuleNotFoundError:没有名为_tkinter的模块'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆