'Python/Tkinter : ModuleNotFoundError: No module named '_tkinter'

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?



Solution 1:[1]

My basic install of linux mint 20 contains python3. On windows machines this includes the tkinter package. My solution was to

sudo apt-get install python-tk

or

sudo apt-get install python3-tk

and live was sunny again.

Solution 2:[2]

Since I couldn't find any solution I just uninstalled my whole environment system (pyenv pyenv-virtualenv virtualenv and virtualenvwrapper) and installed conda instead. Now everything works. Probably I messed up with pyenv installation.

Thank you all :D

Solution 3:[3]

For future people having this problem...

I'm using Catalina and had been getting the error:

ModuleNotFoundError: No module named '_tkinter'

when trying to import certain modules. For instance, I'd get the error when trying to import pdf2docx, even though it had installed okay.

I uninstalled all versions of python 3.x and followed nickolay's step-by-step advice above.

Now it works like a charm! Thanks nickolay!

Solution 4:[4]

mac Monterey (M1)

brew install python-tk

python 3.9.10

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohnish
Solution 2 TĂșlio Chiodi
Solution 3 Tom Greg
Solution 4 Charli Kindly