5

I just upgraded to python 3.9 (Earlier I had python 3.8.2)

I was working on a project where I had to use tkinter module.

Everything worked fine in Python3.8.2

pic1

But after upgrading to Python3.9 , I get this error :

pic2

The error is :

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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'

So anyone knows how to fix this ?

P.S :

I have tried using

sudo apt install python3-tk

It only gives this message :

Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-tk is already the newest version (3.8.2-1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Edit :

I found 2 pip installed on my system namely : pip3 and pip3.9 (I don't know how)

One is familiar pre existing pip3

When I do pip3 list

Package                  Version
------------------------ --------------------
aiohttp                  3.6.2
apturl                   0.5.2
astroid                  2.4.2
async-timeout            3.0.1
attrs                    20.2.0
autopep8                 1.5.4

#(And many more...)

But when I do pip3.9 list , I get only 2 modules installed:

Package    Version
---------- -------
pip        20.2.3
setuptools 41.2.0
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 2
    I think you need to upgrade system package `python3-tk` as well. – acw1668 Oct 05 '20 at 10:52
  • 2
    No, tkinter isn't renamed. – Bryan Oakley Oct 05 '20 at 14:04
  • 2
    If you have `tcl` and `tk` development packages installed, `tkinter` will be built properly when you build Python from source. See my [result](https://imgur.com/3rUQdyx). – acw1668 Oct 05 '20 at 16:49
  • 1
    @acw1668 , and how to do that? –  Oct 05 '20 at 17:53
  • 3
    Do you build Python 3.9 from source? If yes, then as I said you have to make sure `tcl-dev` and `tk-dev` packages are installed before building Python 3.9. – acw1668 Oct 05 '20 at 23:22
  • 1
    I would suggest you uninstall Python 3.9 and [install it with apt application manager](https://realpython.com/installing-python/#how-to-install-python-on-linux) rather than building it from the source – Jdeep Oct 06 '20 at 03:34
  • Does this answer your question? [Why does tkinter (or turtle) seem to be missing or broken? Shouldn't it be part of the standard library?](https://stackoverflow.com/questions/76105218/why-does-tkinter-or-turtle-seem-to-be-missing-or-broken-shouldnt-it-be-part) – Karl Knechtel Apr 26 '23 at 20:13

2 Answers2

1

Okay , Got it what was wrong (Thanks to @acw1668 ). I installed python 3.9 from the source without having dependencies (tk-dev and tcl-dev)

  • So the first thing I did was uninstalling Python3.9
  • Then reinstalled it using this method (From deadsnakes ppa)
  • Then in terminal , run sudo apt install python3.9-tk
  • Now everything works fine.

enter image description here

Thanks to @Noah-J-Standerson for suggesting this method

  • 1
    I have precisely the same problem, and this didn't fix it. When I try `sudo apt install python3.9-tk` it tells me. `Note, selecting 'python3-tk' instead of 'python3.9-tk'` and then it says `python3-tk is already the newest version (3.8.10-0ubuntu1~20.04)` and doesn't do anything. – saulspatz Mar 17 '22 at 02:05
0

For macOS,

Based on your python version, you need to install python-tk.

Example: For python 3.11, I installed tkinter using:

 brew install python-tk@3.11
Lokesh Soni
  • 37
  • 2
  • 8