0

I tried running a python script which uses matplotlib. I got following error:

Traceback (most recent call last):
  File "q3.py", line 6, in <module>
    matplotlib.use('TkAgg')
  File "/home/raj/raj/venvs/notes-bash-venv/lib/python3.8/site-packages/matplotlib/__init__.py", line 1162, in use
    plt.switch_backend(name)
  File "/home/raj/raj/venvs/notes-bash-venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 276, in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
  File "/home/raj/raj/venvs/notes-bash-venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 277, in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/raj/raj/venvs/notes-bash-venv/lib/python3.8/site-packages/matplotlib/backends/backend_tkagg.py", line 1, in <module>
    from . import _backend_tk
  File "/home/raj/raj/venvs/notes-bash-venv/lib/python3.8/site-packages/matplotlib/backends/_backend_tk.py", line 6, in <module>
    import tkinter as tk
ModuleNotFoundError: No module named 'tkinter'

So I tried installing tkinter:

$ pip3 install tkinter
ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none)
ERROR: No matching distribution found for tkinter

What cound be wrong? For the same script, I installed numpy, pandas and matplotlib using command like pip3 install numpy. I am on python 3.8.2 and pip-20.2.3. Also I am on Ubuntu 20.04 on WSL2.

MsA
  • 2,599
  • 3
  • 22
  • 47

2 Answers2

0

Did you try to install dependencies for Tkinter systemwide?:

sudo apt-get install python3-tk

Dmitriy Kisil
  • 2,858
  • 2
  • 16
  • 35
  • I just ran `apt-get install python-tk` as suggested [here](https://stackoverflow.com/a/41523352/6357916). Should I try your command too? – MsA Sep 19 '20 at 08:16
  • @anir, `python-tk` maybe not the same as `python3-tk`(different python versions) – Dmitriy Kisil Sep 19 '20 at 08:17
  • ok ended up getting `File "/home/raj/raj/venvs/notes-bash-venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 284, in switch_backend raise ImportError( ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running` – MsA Sep 20 '20 at 11:27
0

You may resolve it by using this command OS-wide:

sudo apt install python3-tk

Check it out and let me know if it works.

Saeed
  • 3,255
  • 4
  • 17
  • 36
  • I just ran `apt-get install python-tk` as suggested [here](https://stackoverflow.com/a/41523352/6357916). Should I try your command (`python3-tk`) too? – MsA Sep 19 '20 at 08:17
  • 1
    `python-tk` works for Python2, while `python3-tk` works for Python3 as far as I know. – Saeed Sep 19 '20 at 08:20
  • ok ended up getting `File "/home/raj/raj/venvs/notes-bash-venv/lib/python3.8/site-packages/matplotlib/pyplot.py", line 284, in switch_backend raise ImportError( ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running` – MsA Sep 20 '20 at 11:27
  • Check this out: https://stackoverflow.com/a/57636427/5790653 – Saeed Sep 21 '20 at 18:28