47

I want to run python code on Ubuntu 14.04, but when I execute it, it gives me the following error message

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    from tkinter import *
ImportError: No module named tkinter
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Mayank Jain
  • 1,057
  • 2
  • 9
  • 6

7 Answers7

72

Try writing the following in the terminal:

sudo apt-get install python-tk

Don't forget to actually import Tkinter module at the beginning of your program:

import Tkinter
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
the_dude
  • 1,004
  • 1
  • 11
  • 21
46

If you're using Python 3 then you must install as follows:

sudo apt-get update
sudo apt-get install python3-tk

Tkinter for Python 2 (python-tk) is different from Python 3's (python3-tk).

gon1332
  • 1,930
  • 1
  • 24
  • 30
16

To get this to work with pyenv on Ubuntu 16.04, I had to:

$ sudo apt-get install python-tk python3-tk tk-dev

Then install the version of Python I wanted via pyenv:

$ pyenv install 3.6.2

Then I could import tkinter just fine:

import tkinter
PaulMest
  • 12,925
  • 7
  • 53
  • 50
  • What is pyenv? My system can't find it. – sureshvv Aug 18 '18 at 12:16
  • 2
    @sureshvv pyenv is a Python version management tool: https://github.com/pyenv/pyenv – PaulMest Aug 18 '18 at 17:22
  • Python version isn't the deciding factor. Having installed `$ sudo apt-get install python-tk python3-tk tk-dev` before installing python is deciding factor here. As suggested in another answer https://stackoverflow.com/a/26358646/953338 – Fosna Apr 03 '20 at 23:35
  • I just used sudo apt-get install python-tk python3-tk tk-dev and it worked – PythonProgrammi Jun 12 '20 at 06:16
4

First, make sure you have Tkinter module installed.

sudo apt-get install python-tk

In python 2 the package name is Tkinter not tkinter.

from Tkinter import *

ref: http://www.techinfected.net/2015/09/how-to-install-and-use-tkinter-in-ubuntu-debian-linux-mint.html

Aman Deep
  • 187
  • 4
  • 11
2

Try:

sudo apt-get install python-tk python3-tk tk-dev

If you're using python3, then Python3 virtual environment(venv) is also required. Use:

sudo apt install python3-venv
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
1

Install the package python-tk like

sudo apt-get install python-tk

That is described (with apt-cache search python-tk as)

Tkinter - Writing Tk applications with Python
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

In Ubuntu 14.04.2 LTS:

  1. Go to Software Center and remove "IDLE(using Python-2.7)".

  2. Install "IDLE(using Python-3.4)".

Try again. This step worked for me.

frfzone
  • 21
  • 1