226

I tried to use the matplotlib package via 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 the problem - I just installed a brand new python 3.5.1.

EDIT: in addition, I also tried to import 'tkinter' and 'Tkinter' - neither of these worked (both returned the error message I mentioned).

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
noamgot
  • 3,962
  • 4
  • 24
  • 44

19 Answers19

254

For Linux

Debian based distros:

sudo apt-get install python3-tk

RPM based distros:

sudo yum install python3-tkinter

For windows:

For Windows, I think the problem is you didn't install complete Python package. Since Tkinter should be shipped with Python out of box. See: http://www.tkdocs.com/tutorial/install.html . Good python distributions for Windows can be found by the companies Anaconda or ActiveState.

Test the python module

python -c "import tkinter"

p.s. I suggest installing ipython, which provides powerful shell and necessary packages as well.

Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
knh170
  • 2,960
  • 1
  • 11
  • 17
  • IT IS WORKING! thanks! (I installed anaconda through the link you put in your post) – noamgot Mar 31 '16 at 08:36
  • 3
    if you're on a CentOS box, the command is `sudo yum install python36u-tkinter.x86_64` – n1c9 Apr 21 '18 at 21:24
  • I am using pycharm and i got the same error regarding package missing. After installing tkinter package matplotlib worked fine. Thank you @knh170 – Debashis Sahoo Jul 31 '18 at 06:31
  • Worked for me with Ubuntu 18.04 using python3 in venv. Just install it: sudo apt-get install python3-tk Then you can test is from python3 shell: from matplotlib import pyplot – serfer2 Nov 13 '18 at 16:14
  • 3
    @knh170 Can we install tkinter using pip? I cannot search anything related to this – Scott Yang Mar 11 '19 at 12:08
  • 2
    Install Ubuntu package `python3-tk` for Python3: `apt update; apt install python3-tk` (change to `python-tk` for Python2) – Dan Anderson Aug 30 '19 at 20:26
  • 1
    For newer Python versions, installing specifically `python3.7-tk`, for example, might be necessary. – eric.mitchell Aug 17 '21 at 20:34
  • Or apt-get install python3.5-tk for python 3.5 – Morten Mar 09 '22 at 11:43
118

you can use

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

if you dont want to use tkinter at all.

Also dont forget to use %matplotlib inline at the top of your notebook if using one.

EDIT: agg is a different backend like tkinter for matplotlib.

markroxor
  • 5,928
  • 2
  • 34
  • 43
  • 2
    This is nice, because due to working with a server I don't require and have X11, but some programs I have installed have a dependency on matplotlib. – rien333 Jun 21 '18 at 21:24
  • 3
    Fantastic! In case anyone is wondering [how this works](https://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear) – Matt Nov 30 '18 at 15:46
  • In my case plots came broken after applying this solution so I run recommended `sudo apt-get install python3-tk` and all get fine – pbaranski Feb 21 '19 at 13:45
  • This solution is for and I quote `if you dont want to use tkinter at all.` – markroxor Feb 22 '19 at 05:44
  • 2
    this solution does'nt work if you want to plot something – Beyhan Gul Mar 09 '19 at 11:38
  • 7
    Just to add to this answer: `agg` is a non-GUI backend, so `plt.show()` will not have any effect. But you can still do `plt.savefig(filename)` and look at the file for quick debugging. – Nico Apr 01 '19 at 10:28
  • 1
    Saves me time from dealing with this tkinter error. – annoying_squid Aug 26 '19 at 15:12
46

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

  1. Go to start menu, type Apps & features,
  2. Search for "python" in the search box,
  3. Select the Python version (e.g. Python 3.8.3rc1(32-bit)) and click Modify,
  4. On the Modify Setup page click Modify,
  5. Tick td/tk and IDLE checkbox (which installs tkinter) and click next.

Wait for installation and you're done.

gdrt
  • 3,160
  • 4
  • 37
  • 56
36

On Centos, the package names and commands are different. You'll need to do:

sudo yum install tkinter

To fix the problem.

razeh
  • 2,725
  • 1
  • 20
  • 27
21

Almost all answers I searched for this issue say that Python on Windows comes with tkinter and tcl already installed, and I had no luck trying to download or install them using pip, or actviestate.com site. I eventually found that when I was installing python using the binary installer, I had unchecked the module related to TCL and tkinter. So, I ran the binary installer again and chose to modify my python version by this time selecting this option. No need to do anything manually then. If you go to your python terminal, then the following commands should show you version of tkinter installed with your Python:

import tkinter
import _tkinter
tkinter._test()
user58419
  • 341
  • 2
  • 4
  • 1
    Correct. A Windows install is the only system where this can happen - as was the case for myself. +1. This is certainly worth noting for users with Windows installs. – marcushobson Apr 18 '17 at 09:59
19

If you are using fedora then first install tkinter

sudo dnf install python3-tkinter

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

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

And add the necessary packages using pip

sage poudel
  • 357
  • 4
  • 14
10

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

On Redhat 7.4 with Python 3.6, the command is sudo yum install rh-python36-python-tkinter

8

For windows users, re-run the installer. Select Modify. Check the box for tcl/tk and IDLE. The description for this says "Installs tkinter"

peteey
  • 420
  • 4
  • 14
8

On Ubuntu, early 2018, there is no python3.6-tk on ubuntu's (xenial/16.04) normal distributions, so even if you have earlier versions of python-tk this won't work.

My solution was to use set everything up 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 needed 3.6 afterall, and the trick was to:

sudo apt install tk-dev

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

./configure
make
make install
Josh.F
  • 3,666
  • 2
  • 27
  • 37
  • 3
    Update: Now there __IS__ a `python3.6-tk`! `sudo apt install python3.6-tk` works here :) – Luis Mar 10 '18 at 21:10
5

For the poor guys like me using python 3.7. You need the python3.7-tk package.

sudo apt install python3.7-tk

$ python
Python 3.7.4 (default, Sep  2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> exit()

Note. python3-tk is installed. But not python3.7-tk.

$ sudo apt install python3.7-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  tix python3.7-tk-dbg
The following NEW packages will be installed:
  python3.7-tk
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 143 kB of archives.
After this operation, 534 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.7-tk amd64 3.7.4-1+xenial2 [143
kB]
Fetched 143 kB in 0s (364 kB/s)
Selecting previously unselected package python3.7-tk:amd64.
(Reading database ... 256375 files and directories currently installed.)
Preparing to unpack .../python3.7-tk_3.7.4-1+xenial2_amd64.deb ...
Unpacking python3.7-tk:amd64 (3.7.4-1+xenial2) ...
Setting up python3.7-tk:amd64 (3.7.4-1+xenial2) ...

After installing it, all good.

$ python3
Python 3.7.4 (default, Sep  2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()
Pedro Rodrigues
  • 2,520
  • 2
  • 27
  • 26
4

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

sudo apt-get install python3.6-tk

instead of

sudo apt-get install python3-tk

Which works for other versions of python3

bertucho
  • 656
  • 4
  • 7
2

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

user1747134
  • 2,374
  • 1
  • 19
  • 26
2

Sometimes (for example in osgeo4w distribution) tkinter is removed.

Try changing matplotlib backend editing matplotlibrc file located in [python install dir]/matplotlib/mpl-data/matplotlibrc changing The backend parameter from backend: TkAgg to something other like backend: Qt4Aggas described here: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

Enrico Ferreguti
  • 438
  • 4
  • 13
1

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

sudo apt-get install python3.7-tk
John
  • 521
  • 2
  • 5
  • 12
1

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

  1. Complie and install tcl/tk from http://www.tcl.tk/software/tcltk/download.html, I'll suppose you installed python at /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 supported, 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
qi yuan
  • 11
  • 4
0

I had the same issue on Win x86/64 because my custom Python3.7 installation did not include Tcl packages, so just modify or re-install your python

https://www.python.org/downloads/release/python-370/

enter image description here

27P
  • 1,183
  • 16
  • 22
0

Download Python Setup file and click modify then tick the tcl/tk and install.

After installation is complete go to folder where python is installed ( Default is C:\Users*Your username*\AppData\Local\Programs\Python\Python39\Lib) .

Copy the tkinter folder and paste it in the lib folder of your pycharm project.

Error should be resolved

0

Follow these steps to easily install Tkinter on your PyCharm IDE:

First go to the File: enter image description here

Second is go to New Project Setup > Settings for new projects: enter image description here

And then click the Settings for new projects and you'll get redirected in here: Please make sure to select the Python Interpreter

Please click the + symbol in there: enter image description here

after that install future and there you go... enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kris Sapin
  • 27
  • 7
-1

If you’re having pip(which you probably do), open up cmd or powershell on Windows or a terminal window on OS X or Linux and try this(make sure python is in the system path if you’re on Windows):

pip install tkinter

It should take a while to install tkinter, and then try to execute this code block:

from tkinter import *
root = Tk()
# Your code goes here 
root.mainloop()

Hope that this helps! Thank you!

InfoDaneMent
  • 326
  • 3
  • 18