15

I've already gone through all the similar questions in this regard and tried the solutions proposed there. But I'm unable to get this error sorted out though my python3-tk package is installed in the proper virtualenv that I'm using for my project.

Though in my project, I don't use tkinter, when i try to run the file, I'm getting the following error related to the _tkinter module.

Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/init.py", line 36, in import _tkinter
ImportError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/manuelanayantarajeyaraj/PycharmProjects/ChatbotWord2Vec/main.py", line 2, in from matplotlib import pyplot as plt
File "/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/pyplot.py", line 115, in _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/backends/init.py", line 62, in pylab_setup [backend_name], 0)
File "/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 4, in from . import tkagg # Paint image to Tk photo blitter extension.
File "/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/backends/tkagg.py", line 5, in from six.moves import tkinter as Tk
File "/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/six.py", line 92, in get result = self._resolve()
File "/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/six.py", line 115, in _resolve return _import_module(self.mod)
File "/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/six.py", line 82, in _import_module import(name)
File "/usr/lib/python3.5/tkinter/init.py", line 38, in raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package

Hence, I navigated to the location of my interpreter and created a virtualenv and installed the python3-tk package using the following

sudo apt-get install python3-tk

When I checked, all the packages seem to be up to date

Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-tk is already the newest version (3.6.5-3~16.04.york0.2).
The following packages were automatically installed and are no longer required:
  libappindicator1 libindicator7 libllvm4.0 linux-headers-4.10.0-28
  linux-headers-4.10.0-28-generic linux-headers-4.13.0-36
  linux-headers-4.13.0-36-generic linux-headers-4.13.0-37
  linux-headers-4.13.0-37-generic linux-image-4.10.0-28-generic
  linux-image-4.13.0-36-generic linux-image-4.13.0-37-generic
  linux-image-extra-4.10.0-28-generic linux-image-extra-4.13.0-36-generic
  linux-image-extra-4.13.0-37-generic linux-signed-image-4.10.0-28-generic
  linux-signed-image-4.13.0-36-generic linux-signed-image-4.13.0-37-generic
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 37 not upgraded.

But I'm still getting the same import error ImportError: No module named '_tkinter', please install the python3-tk package.

Any suggestions in this regard will be highly appreciated.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
  • 1
    show please output of `dpkg -s python3-tk`. Probably you have installed python3-tk for python3.6. but you need for python3.5 – Konstantin May 14 '18 at 10:33
  • 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 19:48
  • "Hence, I navigated to the location of my interpreter and created a virtualenv and installed the python3-tk package using the following" - here is where the problem is. Instead, create the virtualenv **after** installing python3-tk. Otherwise, the virtualenv may have created a copy of the old environment that does not support Tk (because the package wasn't installed yet). – Karl Knechtel Apr 26 '23 at 19:49

2 Answers2

29

When you import matplotlib, it will probably be trying to use the tk backend as the default. If you didn't install tk, or you do not want to use it anywhere else in your project, then a possible solution would be to simply use a different backend:

import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
DavidG
  • 24,279
  • 14
  • 89
  • 82
  • Thanks a million. This solved my issue. I really appreciate it. But am not able to visually see the plots. I have a few `plt.show()` which run perfectly on jupyter notebook. Where can I expect to see the same plots as I run my `main.py` file? – Nayantara Jeyaraj May 14 '18 at 11:10
  • "agg" is a non-GUI backend, so you will need to save to a file. – Klaifer Garcia Sep 17 '19 at 13:29
14

The message indicates that when you run sudo apt-get install python3-tk it tells you that tkinter is sintalled for Python3.6.5, but on the other hand, the ImportError is related to Python3.5. So I believe this should resolve your problem:

sudo apt-get install python3.5-tk
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • 1
    I tried this and am getting the following output. `Building dependency tree Reading state information... Done Package python3.5-tk is a virtual package provided by: python3-tk 3.5.1-1 [Not candidate version] E: Package 'python3.5-tk' has no installation candidate` – Nayantara Jeyaraj May 14 '18 at 11:05
  • 4
    I had the opposite problem, but knowing it's possible to specify sub version solved the problem for me. On Ubuntu 16, the default python 3 version is 3.5. On Ubuntu 16, you have to spend a little effort to get 3.6 running, which I did. However, the default tk version stayed 3.5. So specifying python3.6-tk let my system install the right tk. – TheGrimmScientist Aug 02 '18 at 15:44