Python/Tkinter:ModuleNotFoundError:没有名为“_tkinter"的模块 [英] Python/Tkinter : ModuleNotFoundError: No module named '_tkinter'

查看:60
本文介绍了Python/Tkinter:ModuleNotFoundError:没有名为“_tkinter"的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 StackOverflow 上的第一篇文章,如果您有任何改进我的文章的建议,我会尽量使其正确和完整,我很乐意接受.

我在运行使用 Tkinter 的 Python 编写的代码时遇到问题.

我将尝试详细描述我的操作,以方便识别错误.

我在 Coursera 上开设了一门关于 DSP(数字信号处理)的课程,建议安装一个用 Python(和一点 C 语言)编写的工具.我正在使用 Arch Linux.

Github 上的链接:

  1. 现在检查 tkinter.在 shell 中运行 python -m tkinter -c "tkinter._test()".您应该会看到如图所示的测试窗口:

就是这样!

我的环境:

检查是否在执行上述步骤时出错:

  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 命令安装)

This is my first post StackOverflow, I will try to make it as correct and complete as possible if you have any tips to improve my post I will gladly accept it.

I'm having trouble running code written in Python that uses Tkinter.

I will try to describe in detail my actions to facilitate the identification of the error.

I started a course at Coursera on DSP (Digital Signal Processing) where it is suggested to install a tool written in python (and a little bit of C). I'm using Arch Linux.

link on Github: sms-tools repo

Using pyenv/virtualenv/virtualenvwrapper I created an environment with Python 3.7.5, as recommended in the "How to use" section of the repository.

I installed the required libraries in my environment by pip:

%pip install ipython numpy matplotlib scipy cython

I compiled some C functions in the "/sms-tools/software/models/utilFunctions_C"

directory with the following command:

%python compileModule.py build_ext --inplace

Finally, I run the models GUI in the directory "/sms-tools/software/models_interface"

%python models_GUI.py

and I get the following message:

Traceback (most recent call last):
  File "models_GUI.py", line 6, in <module>
    from Tkinter import *   ## notice capitalized T in Tkinter 
ModuleNotFoundError: No module named 'Tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "models_GUI.py", line 9, in <module>
    from tkinter import *   ## notice lowercase 't' in tkinter here
  File "~/.pyenv/versions/3.7.5/lib/python3.7/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

I will now describe some of my attempts to solve the problem:

Looking at Tkinter section in Python Wiki I tried installing Tcl and Tk.

%sudo pacman -S tk

but it was already installed. after that I tried installing with pip:

%pip install tk

and

%pip install tkinter

and the error remains the same.

I also tried to create a symlink with this code: %ln -s /usr/lib/python3.8/lib-dynload/_tkinter.cpython-38-x86_64-linux-gnu.so _tkinter.cpython-38-x86_64-linux-gnu.so

the symlink was created in the following folders:

~/.ve/Coursera_DSP/lib/python3.7/lib-dynload

and

.pyenv/versions/3.7.5/lib/python3.7/lib-dynload

But I still get the same error.

I appreciate it if anyone has any suggestions and I apologize for the language errors since English is not my mother tongue.

After an incessant search on the internet, I believe the problem is related to pyenv and TCL/TK. I don't understand much about the subject but I suspect that in the creation of the environment by virtualenv python has lost the connection with TCL/TK. Does that make any sense?

解决方案

Here is step by step guide to make IDLE and tkinter work. Working for me on macOS Catalina. Should be easily adapted to Linux environment:

  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. 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"

  6. 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
  7. 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
  8. finally install Python with pyenv with pyenv install <version>. E.g. pyenv install 3.8.2

Test

  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. now check tkinter. In shell run python -m tkinter -c "tkinter._test()". You should see test window like on the image:

That's it!

My environment:

check this is something went wrong executing steps above:

  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)

这篇关于Python/Tkinter:ModuleNotFoundError:没有名为“_tkinter"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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