8

I have Python 3.8 on Ubuntu 16.04.

I installed python3-tk (it is required for showing plots in matplotlib):

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

and python3.8-tk:

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

But it is not found:

$ python3.8 -m tkinter
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/runpy.py", line 184, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/local/lib/python3.8/runpy.py", line 143, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/local/lib/python3.8/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "/usr/local/lib/python3.8/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'

What is the correct way to install it?

1
  • sudo apt-get install tk8.6-dev – RCvaram May 11 '20 at 12:53
1
+50

Recompile and reinstall python3.8 specifying path to folders with tcl, tk includes and libraries.

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev python-tk python3-tk tk-dev

cd ~/Downloads
wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
tar -xvf Python-3.8.2.tgz

cd Python-3.8.2

Edit ./configure file the next way: replace ... for next lines:

  --with-tcltk-includes='-I/usr/include'
  --with-tcltk-libs='-L/usr/lib'

./configure
make -j2    # replace 2 by number of processor cores you have
sudo make install

$ python3.8
Python 3.8.2 (default, May 11 2020, 14:30:03) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 

Python 3.8 installed through apt and pyenv on 16.04 does not include tkinter, as I think or there's a some bug that does not allow to import it. Only rebuilding helped. Previously I've used 20.04 with built in Python 3.8 version, which supports tkinter with only additional packages installing as it is for Python 3.5 on 16.04.

python3.8-tk installation as Ankur A Sharma said is also required. I've forget to mention it. But it is not sufficient for 16.04, at least in my case.

Additional requirement from OP's comment:

sudo ./configure --with-tcltk-includes='-I/usr/include -I/usr/include/tcl' --with-tcltk-libs='-L/usr/lib -ltcl -ltk' --enable-optimizations
2
  • 1
    I tried this and got a compilation error saying that it cannot find tcl.h. Following this answer I added -I/usr/include/tcl to the configure command, and indeed the compilation worked, but I got a link error. So I changed the configure command to sudo ./configure --with-tcltk-includes='-I/usr/include -I/usr/include/tcl' --with-tcltk-libs='-L/usr/lib -ltcl -ltk' --enable-optimizations. Now finally it works! Thanks a lot. – Erel Segal-Halevi May 12 '20 at 7:14
  • Ok. I've also tried similar compilation command, but thought it didn't help decided to not include it to answer as useless. But it seems I was wrong. Thanks. – Gryu May 12 '20 at 7:44
5

If you installed python3.8 using apt (via ppa:deadsnakes/ppa), it can be installed using apt too, the name of library is python3.8-tk.

sudo apt install python3.8-tk

In my case, it solves the problem. For instance, now I can use matplotlib in python3.8 which requires tkinter.

1
  • 1
    This is by far the simplest, hence the best, solution. – user1928764 Nov 6 '20 at 7:20
0

open terminal type command

''pip3 install tkintertable''

it can be pip or pip3 according to your python version

2
  • I installed tkintertable using pip3, but the problem remains – Erel Segal-Halevi May 10 '20 at 8:17
  • switch back to python 3.6 – Shah Vipul May 12 '20 at 7:51
0

On Ubuntu 16.04, (uninstall python3.8 and tkinter, and) install using the ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.8 python3-tk

On Ubuntu 18.04 and above:

sudo apt install python3.8 python3-tk
1
  • I have 16.04 I removed python3.8 and python3.8-tk and reinstalled as you suggested, but the problem remains.. – Erel Segal-Halevi May 12 '20 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.