This problems happens when I run Python code which uses “import matplotlib.pyplot as plt” or “import axelrod as axl”. Both of them use “tk”.
The full error:
1 | >>> import matplotlib.pyplot as plt |
I use “pyenv” to manage my local Python environments.
I searched a lot, most of the result said that installing “python3-tk”, “tk”, “python-tk” or “tk-dev” will fix the issue. But for my case, no. Until I found the answer here and here.
The fix is:
Install “tk-dev”, for me it’s “sudo apt-get install tk-dev”;
Uninstall the existing Python environment of pyenv and install it again, which will use “tk-dev” to compile Python and the missing “_tkinter” module will be compiled.
1 | sudo apt-get install tk-dev |
Done.