28

I am stuck with this issue since last two days and I have tried every possible solution on the stack and github. It will be really great if someone can recommend.

I am working with python 2.7 in a virtual environment on CentOS Linux release 7.3.1611.

I am running a script that uses matplotlib.pyplot and on run gives this error

import  matplotlib.pyplot as plt
 File "/usr/local/packages/Python-2.7/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter 

I tried to install tkinter using -

 pip install tkinter and it gave this error 
Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter

then I even installed -

sudo yum install tk
sudo yum install tk-devel
sudo yum install tc 

and it says packages are already installed and nothing to to

I have set up my virtual environment again to see if I missed something but I cannot get anywhere. Please help!

AnkP
  • 631
  • 2
  • 9
  • 18
  • 1
    on the newest Ubuntu I had to install `python-tkinter` using `apt-get` (similar to `yum`). `tk` and `tcl` are used by `tkinter` but they don't install `tkinter` in `python` – furas Dec 21 '16 at 16:54
  • maybe check if `yum` has command `search/find` and try to find `tkinter` (not `tk`) – furas Dec 21 '16 at 16:55
  • @furas , yes I read it in multiple forums, but when I try to install `tk` and `tcl` it tells me there is nothing to do! – AnkP Dec 21 '16 at 16:55
  • it gives me `tk-8.5.13-6.el7.x86_64` is already installed and on yum search for tkinter it returns - `python2-tktable.x86_64 : TkTable wrapper for Python 2.x with Tkinter python34-tkinter.x86_64 : A GUI toolkit for Python 3 tkinter.x86_64 : A graphical user interface for the Python scripting language` – AnkP Dec 21 '16 at 16:58
  • 1
    `tk/tcl` is not `tkinter`. `tkinter` is wrapper which needs language `tk/tcl` to work but `tk/tcl` doesn't need `tkinter` to run (so it doesn't add `tkinter` to `Python`) – furas Dec 21 '16 at 16:59
  • maybe you have to install `python34-tkinter` but it seems it is for Python 3.4 – furas Dec 21 '16 at 17:00
  • Try `sudo yum reinstall tkinter`. – acw1668 Dec 22 '16 at 07:20
  • Tray `import Tkinter` instead of `import tkinter` in python 2.7 – Soheil Paper May 25 '19 at 07:00

7 Answers7

41

Try this

sudo apt-get install python3-tk

this worked for me

rath3r
  • 323
  • 1
  • 6
  • 19
Harsh Mathur
  • 528
  • 4
  • 6
  • 2
    I needed sudo apt-get install python3.6-tk – Yuri Feldman Nov 08 '18 at 14:26
  • 1
    Why can't this module be included with pip? – yamanidev Feb 10 '22 at 22:37
  • @yamanidev because it is **not a Python package**. It will reconfigure the system so as to ensure that Tcl/Tk (which is pure C code that can be used in other contexts) is installed and accessible from Python, *in addition to* installing the `_tkinter` module in the right place. Also, because it modifies the system Python, where **Pip may have been deliberately disabled**. – Karl Knechtel Apr 26 '23 at 19:29
1

Try with apt-get install python-tk

1

had same issue: yum install tkinter for CentOS and RedHat for python 2.x!

Roy Holzem
  • 860
  • 13
  • 25
1

For people still experiencing this issue, try to change your Python project interpreter. Tkinter should be included by default, but it is possible that your distribution does not have it included. Always download the latest base interpreter from the official site.

0

I had the same error as you, I try installing dependencies, tk/tcl, but it didn't work. Finally I solved it using:

sudo apt-get update
sudo apt-get install python-tk

on ubuntu16.04

Mickael Maison
  • 25,067
  • 7
  • 71
  • 68
jacker_wei
  • 41
  • 3
0
sudo yum install python-tools -y

This worked for me on Amazon Linux 2.

mellifluous
  • 2,345
  • 2
  • 29
  • 45
-2

Since you are using python 2.7 tkinter is Tkinter. Try importing Tkinter.

https://docs.python.org/2/library/tkinter.html

  • so if I got to python prompt and use import Tkinter, i get File `"/usr/local/packages/Python-2.7/lib/python2.7/lib-tk/Tkinter.py", line 39, in import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named _tkinter` – AnkP Dec 21 '16 at 16:52
  • also the path for python in my virtual env is `/home//CVD_venv/lib/python2.7/` not `/usr/local/packages/Python-2.7/lib/python2.7/` so python still looks for this module in the root/central location? – AnkP Dec 21 '16 at 16:54
  • You probably tried this but on this page http://stackoverflow.com/questions/36327134/matplotlib-error-no-module-named-tkinter – Edward Park Dec 21 '16 at 16:59
  • 1
    "On CentOS 6.5 with python 2.7 I needed to do: yum install python27-tkinter" – Edward Park Dec 21 '16 at 17:00
  • Yes I did try this earlier - it gives me this message `No package python27-tkinter available. Error: Nothing to do` – AnkP Dec 21 '16 at 17:03
  • i did `sudo apt-get install python27-tkinter` but i get `E: Unable to locate package python27-tkinter` mybe you must instal it in python3 by this `sudo apt-get install python3-tk` i did it and worked. in python 2.7 you coud use `import Tkinter`. – Soheil Paper May 25 '19 at 06:57