'Tkinter unable to configure

I have this problem when I try to run an app that imports turtle:

Traceback (most recent call last):
  File "/home/panos/Documents/python/challenge_60.py", line 1, in <module>
    import turtle
  File "/usr/local/lib/python3.9/turtle.py", line 107, in <module>
    import tkinter as TK
  File "/usr/local/lib/python3.9/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

I have done the below commands:

sudo apt-get install python3-tk 
pip3 install tkinter
pip3 install PythonTurtle
pip3 install Tcl
sudo apt-get install python-tk python3-tk tk-dev
sudo apt-get install python3-PythonTurtle

And, although it is installed, the error still exists. I haven't understand what does wiki says. Could you explain, with all the details, what do I have to do, because I am a newbie at Linux and commands in general?



Solution 1:[1]

  • This error is probably made, because you have installed the tkinter , for instance, on python 3.7, but you are running your code with an interpreter based on python 3.8.
  • Another reasons, like installing the library in a virtual environment and trying to run the code out of the venv, can also lead to this error.

Make sure pip and python command are in the same location/path
to make sure they are from the same SDK.

  • This command indicates where python is installed on your machine: (Unix Only)
which python

Windows users, use this snippet below to find path of the installed python :

import sys
locate_python = sys.exec_prefix
print(locate_python)
  • Check the pip location using the which command as well.
which pip

If pip and python are in the same path, you probably won't get the error anymore.

Solution 2:[2]

First you have to pay attention to a few things 1- All the packs must be updated and installed If you have downloaded the packs, update them, especially the pip module that is for installation 2- Note that the spelling of the words you use is correct 3 - Maybe the code you write does not support that module, you should also pay attention to this issue 4- Finally, there are some problems in your code. First, to download the Tkinter module, you have to write pip3 install tk or pip install tk. You should not write the word Tkinter yourself. Update the latest version and then try again. If it still does not work, turn on your Windows 10 (if not) and finally the last solution is to change your ide vs code. The best ide for this situation. Is And you can also help to install and import the library of online articles. If you use pycharm, enter the Windows command line and type pip3 install future or pip install future, and then the thinner will be imported for you, and then enter the following command: from future.moves import tkinter and you can find how to install each house in different ideas with a simple search

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
Solution 2 py_programmer