4

I tried to run a code which use turtle to draw a tree. There is no problem with code because I ran it on Windows 10 successfully, but on Ubuntu, the following error was encountered:

mosharraf@mmh:~$ cd Desktop
mosharraf@mmh:~/Desktop$ python3 Tree.py
Traceback (most recent call last):
  File "Tree.py", line 6, in <module>
    import turtle 
  File "/usr/local/lib/python3.6/turtle.py", line 107, in <module>
    import tkinter as TK
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
mosharraf@mmh:~/Desktop$

Then I tried to install tkinter but I failed.

mosharraf@mmh:~$ sudo apt-get install python3-tk
[sudo] password for mosharraf: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-tk is already the newest version (3.5.1-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
mosharraf@mmh:~$

Afterwards, I checked for the tkinter in the directories and found it.

mosharraf@mmh:/usr/local/lib/python3.6$ cd tkinter
mosharraf@mmh:/usr/local/lib/python3.6/tkinter$ ls
colorchooser.py  constants.py  dnd.py         font.py      __main__.py    __pycache__      simpledialog.py  tix.py
commondialog.py  dialog.py     filedialog.py  __init__.py  messagebox.py  scrolledtext.py  test             ttk.py
mosharraf@mmh:/usr/local/lib/python3.6/tkinter$ python3
Python 3.6.4 (default, Dec 22 2017, 18:44:45) 
[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>
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
>>> 

tkinter is in the directories as you can see above, but I can't import it. I tried several ways like

import tkinter

import tkinter as tk

from tkinter import *

etc.

Note that I ran the code in python 2.7 and it ran successfully. I also ran the code on Windows 10, Python 3.6. But in Ubuntu, it's causing the problem.

  • 1
    Please add code and the error messages to your question – Nico Haase Dec 28 '17 at 17:57
  • See [this](https://stackoverflow.com/q/5459444/7032856). – Nae Dec 28 '17 at 18:21
  • I've seen this before asking my question. It didn't help me. – Mosharraf Hosain Dec 28 '17 at 18:24
  • @MosharrafHosain You are supposed to put your attempts to resolve the issue too. – Nae Dec 28 '17 at 18:25
  • I'd put more info on why the proposed answers in the linked question & answers failed. – Nae Dec 28 '17 at 18:29
  • How did you install python3? "_tkinter" is not a directory, it's an .so file. I suspect you have several versions of python3 installed. Use the command `whereis python3` so see. I suspect you have installed tkinter to python3.5, not the 3.6 version that you installed with a ppo. You can test this by launching the other versions of python (ie use the command `python3.5`) and seeing if tkinter works there. If so, you could try `sudo apt-get install python3.6-tk` or you could try to make a symbolic link from the python 3.6 directory to the .so file in the 3.5 directory. – Novel Dec 28 '17 at 19:32
  • Or just use the version of python3 that Ubuntu provides, I'm guessing python3.5. Do you really need 3.6? – Novel Dec 28 '17 at 19:34
  • @Novel, I knew that Ubuntu comes with Python 2.7. That's why I installed Python 3.6. But later I discovered that Ubuntu comes with Python 3.5 too. I tried a long time fix the problem, but in vein. That's why I deleted Ubuntu and reinstalled it. Now it is alright. However, this may be helpful to others. Thank you. – Mosharraf Hosain Dec 30 '17 at 14:29

1 Answers1

1

At the bottom of your post you say:

Note that I ran the code in python 2.7 and it ran successfully.

When you import Tkinter from Python 2 you must say:

import Tkinter

When you import Tkinter from Python 3 you must say:

import tkinter

A snippet I use for Tkinter when I know that my script is going to be ran from multiple versions of Python is below:

import sys

py_version = int(sys.version[0])

# If the python version is lower than 3, use Python 2 import
if py_version < 3:
    import Tkinter

# Otherwise use Python 3 import 
else:
    import tkinter

IF THE ABOVE DID NOT FIX YOUR PROBLEM:

I see that in one of your error messages it says

python3-tk is already the newest version (3.5.1-1).

But the error message also says that your Python version is 3.6.

Try using: sudo apt-get install python3.6-tk

Jebby
  • 1,845
  • 1
  • 12
  • 25
  • I tried these all. But in vein. I deleted my Ubuntu and reinstalled it. Now it is alright. Anyway, thanks to you. This can be helpful to others. As my reputation is below 15, I can't vote up your answer. – Mosharraf Hosain Dec 30 '17 at 14:33
  • Sorry to hear about the need to reinstall, but I am glad that you were able to get things working. – Jebby Dec 30 '17 at 14:54