Matplotlib error - no module named tkinter

I tried using the matplotlib package through the Pycharm IDE on Windows 10. when I run this code:

from matplotlib import pyplot 

I get the following error:

 ImportError: No module named 'tkinter' 

I know that in python 2.x it was called Tkinter, but that is not a problem - I just installed the new python 3.5.1.

EDIT: in addition, I also tried importing 'tkinter' and 'Tkinter' - none of them worked (both returned the error message that I mentioned).

+169
python matplotlib tkinter
Mar 31 '16 at 7:42
source share
14 answers
 sudo apt-get install python3-tk 

Then

 >> import tkinter # all fine 

Edit

For Windows, I think the problem is that you did not install the full Python package. Since Tkinter has to be shipped from Python out of the box. See: http://www.tkdocs.com/tutorial/install.html

I suggest installing ipython , which also provides powerful shells and necessary packages.

+191
Mar 31 '16 at 7:53
source share
β€” -

you can use

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

if you don't want to use tkinter .

Also remember to use %matplotlib inline at the top of your laptop if you use it.

EDIT: agg is another backend, like tkinter for matplotlib.

+64
Apr 23 '18 at 19:49
source share

In Centos, package names and commands are different. You will need to do:

 sudo yum install tkinter 

To fix the problem.

+34
Oct 07 '16 at 14:21
source share

If you are using fedora, first install tkinter

 sudo dnf install python3-tkinter 

I don’t think you need to import tkinter afterwards I also suggest you use virtualenv

 $ python3 -m venv myvenv $ source myvenv/bin/activate 

And add the necessary packages with pip

+18
Feb 12 '17 at 8:08
source share

Almost all the answers I was looking for on this issue suggest that Python on Windows comes with tkinter and tcl already installed, and I was out of luck trying to download or install them using pip or actviestate.com. In the end, I found that when I installed python using the binary installer, I unchecked the module associated with TCL and tkinter. So, I ran the binary installer again and decided to change the version of Python by selecting this option. Then you do not need to do anything manually. If you log into your Python terminal, the following commands should show you the version of tkinter installed with your Python:

 import tkinter import _tkinter tkinter._test() 
+17
Mar 26 '17 at 17:22
source share

On CentOS 7 and Python 3.4, the sudo yum install python34-tkinter

In Redhat 7.4 with Python 3.6, the command looks like this: sudo yum install rh-python36-python-tkinter

+9
May 05 '17 at 6:23 a.m.
source share

In Ubuntu, in early 2018, normal Ubuntu distributions (xenial / 16.04) do not have python3.6-tk , so even if you have earlier versions of python-tk this will not work.

My solution was to use install everything with python 3.5 :

  sudo apt install python3.5-tk virtualenv --python='which python3.5' python-env source python-env/bin/activate pip install -r requirements.txt 

And now matplotlib can find tkinter .

EDIT :

I just need 3.6 after all, and the trick was:

 sudo apt install tk-dev 

and then rebuild python3.6 after tk-dev , for example:

 ./configure make make install 
+7
Jan 09 '18 at 0:44
source share

For Windows users, run the installer. Select "Edit." Check the box for tcl / tk and IDLE. The description for this says: "Installs tkinter"

+6
Jun 08 '17 at 20:19
source share

If you are using python 3.6, this worked for me:

 sudo apt-get install python3.6-tk 

instead

 sudo apt-get install python3-tk 

What works for other versions of python3

+4
Sep 20 '17 at 10:42 on
source share

For Windows users , there is no need to download the installer again. Just do the following:

  1. Go to the "Start" menu, enter " Programs and Features" ,
  2. Choose a version of Python (for me, this is Python 3.6.5 (64-bit ) ),
  3. Right click, click Modify ,
  4. Click Change .
  5. Select td / tk and IDLE (which installs tkinter) and click Next .

Wait for the installation, and you're done.

+3
Apr 16 '18 at 17:42
source share

On CentOS 6.5 with python 2.7 I needed to do: yum install python27-tkinter

+2
Nov 11 '16 at 11:53 on
source share

Sometimes (for example, in the osgeo4w distribution) tkinter is deleted.

Try changing the matplotlibrc file for the matplotlib archiving located in [python install dir]/matplotlib/mpl-data/matplotlibrc by changing the backend parameter from backend: TkAgg to something else like backend: Qt4Agg , as described here: http: // matplotlib.org/faq/usage_faq.html#what-is-a-backend

+2
Nov 16 '17 at 10:51 on
source share

Since I'm using Python 3.7 on Ubuntu, I had to use:

 sudo apt-get install python3.7-tk 
0
Aug 17 '18 at 12:43
source share

Maybe you installed Python from the source. In this case, you can recompile python with tcl / tk support.

  1. Complete and install tcl / tk from http://www.tcl.tk/software/tcltk/download.html. I suppose you installed python in /home/xxx/local/tcl-tk/ .
 # install tcl wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz tar -xvzf tcl8.6.9-src.tar.gz cd tcl8.6.9 ./configure --prefix=/home/xxx/local/tcl-tk/ make make install # install tk wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz tar -xvzf tk8.6.9.1-src.tar.gz cd tk8.6.9.1 ./configure --prefix=/home/xxx/local/tcl-tk/ make make install 
  1. Recompile python with tcl / tk support, for example:
 # download the source code of python and decompress it first. cd <your-python-src-dir> ./configure --prefix=/home/xxx/local/python \ --with-tcltk-includes=/home/xxx/local/tcl-tk/include \ --with-tcltk-libs=/home/xxx/local/tcl-tk/lib make make install 
0
Apr 12 '19 at 1:59
source share



All Articles