Solution to matplotlib error – no module named tkinter in Python

matplotlib error – no module named tkinter in Python is a very common error which occurs because of a faulty tkinter installation or installation method.

This post is a guide showing you why you are having this error and how you can get rid of it in the most efficient way possible, I will also include some alternative solutions that may help you.

Exploring the Error : matplotlib error – no module named tkinter in Python

The error happens when you try to install tkinter while using a faulty tkinter installation or installation method.

Bellow, is how the error should look like. Make sure your error message is very similar or an exact match of this error message.

                                                                       #
ImportError: No module named 'tkinter'
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution 1 : use agg instead of tkinter

If you desire, You can refrain from using tkinter entirely and replace it with agg.

You should add the code bellow. And if you are using a notbook at this to the top %matplotlib inline

                                                                       #
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
                                                                       #

Its worth to note that for quick debugging using agg, you can use plt.savefig(filename).

This solution should be enough for most people. If it was not helpful try the solution bellow.

Solution 2 : Correctly install tkinter.

The problem is an installation problem. The link bellow is a good starting point to get the right package for windows.

http://www.tkdocs.com/tutorial/install.html

You can install Anaconda or ActiveState and that will fix your problem.

You can also use the command bellow or maybe use ipython instead

                                                                       #
python -c "import tkinter"
                                                                       #

For Linux, you can use

                                                                       #
sudo yum install python3-tkinter
                                                                       #

or alternatively the command bellow

                                                                       #
sudo apt-get install python3-tk
                                                                       #

I hope the fix above fixed your problem, good luck with the scripts to come.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

This is it, it is the end of our article, I hope I helped you solve this Python error, Do not give up, keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/