16

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?

5 Answers5

16

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.

sugab
  • 4,417
6

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
Gryu
  • 8,002
  • 9
  • 37
  • 53
1

on ubuntu 20.4 to 21.4 This will work

sudo apt-get install python3-tk

0

open terminal type command

''pip3 install tkintertable''

it can be pip or pip3 according to your python version

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