9

I'm running Windows 7 32-bit. I've installed Python 3.2.2 and selected every module for installation (including Tcl/Tk). On my computer, I can run a script by double-clicking the .py file and it will find my Tkinter import just fine. If I run it from a command line, it says ImportError: No module named 'Tkinter'. I passed this script on to a coworker who also installed the same way, and she can't run the script at all even with double-clicking. Same Tkinter problem. Our PATHs are identical with C:\Python33 being the first item and tkinter shows in the lib folder. I'm running out of ideas. What's going on? Why is Tkinter so finicky with existing?

Update: Apparently Tcl/Tk do not include Tkinter. The reason it worked for me was because I had installed a special Python package via our company's download system that happened to include it. This version was linked to .py extensions. In command prompt, however, my updated Python (with Tcl/Tk but without Tkinter) was the python of choice as selected by my PATH variable. My coworker did not have this special package installed so it did not work for her. I had thought it was my Python 3.3 that was running the script but it was not which is why it seemed like it worked for me. That said, if anyone else runs into this issue, check out the sys.executable and sys.version as indicated below to figure out just what is going on!

falsetru
  • 357,413
  • 63
  • 732
  • 636
CodeMonkey
  • 1,795
  • 3
  • 16
  • 46

2 Answers2

15

You may have both Python 2.x and Python 3.x. And py extension is linked to Python 2.x interpreter. And your python script is designed to run with Python 2.x.

In Python 3, Tkinter module was renamed to tkinter (lowercase).


Make a script as follow, then run it by clicking it, and run it in command. You may get different results:

import sys
print(sys.version)
input()
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Typing "python" in a command prompt shows: Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 – CodeMonkey Sep 26 '13 at 14:12
  • @CodeMonkey, I updated the answer. Once, run it by double-clicking and run it in command. – falsetru Sep 26 '13 at 14:15
  • But we both do have two versions installed. As for capitalization... it turns out that my tkinter folder contains neither tkinter nor Tkinter. So... where's it really coming from? – CodeMonkey Sep 26 '13 at 14:17
  • @CodeMonkey, It's in `C:\python3.3\Lib\tkinter` (Python 3.3), `C:\Python27\Lib\lib-tk\Tkinter.py` if you didn't changed the installation directory. – falsetru Sep 26 '13 at 14:19
  • @CodeMonkey, Try `python -c "import tkinter; print(tkinter.__file__)"` or `python -c "import Tkinter; print(Tkinter.__file__)"`. Then, you can find where the module come from. – falsetru Sep 26 '13 at 14:20
  • Very very interesting... That might get me on the right path :-) 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] – CodeMonkey Sep 26 '13 at 14:21
  • I removed Python33 from PATH and now command prompt shows it is using 2.6.5. However, it's still not finding Tkinter. So basically, same problem – CodeMonkey Sep 26 '13 at 14:25
  • I've added Python33 back to PATH and imported tkinter instead of Tkinter. Still doesn't find it in command prompt. I changed it so .py opens with Python33 and it doesn't work either. – CodeMonkey Sep 26 '13 at 14:30
  • @CodeMonkey, What do you get with the following command in command line? `python -c "import Tkinter; print(Tkinter.__file__)"` – falsetru Sep 26 '13 at 14:35
  • ImportError: No module named 'Tkinter' – CodeMonkey Sep 26 '13 at 14:36
  • @CodeMonkey, I think you installed Python 2.6 without Tkinter. If possible, reinstall Python 2.6 with Tkinter. – falsetru Sep 26 '13 at 14:37
  • I have a mystical "Python Launcher For Windows" application that runs my scripts on double-click. It uses python 2.6.5 and is the only application that works. If I select any other python.exe (even 2.6.5), the script fails. Typing the application in Start>Search, it claims it resides in C:\Python33 and that it's really python.exe. – CodeMonkey Sep 26 '13 at 14:39
  • 1
    @CodeMonkey, Make a script with `import sys; print(sys.executable)` as content. Run it in command line and by using "Python Launcher For Windows". And please let me know result of both run. – falsetru Sep 26 '13 at 14:42
  • Well, it printed out yet another version of python (these machines have way too much stuff installed :-p). Using that version for command prompt actually works! Oddly enough, however, selecting the version directly for opening .py files does not work. At any rate, I believe with all this info combined, it has resolved my issue. Thanks!! – CodeMonkey Sep 26 '13 at 17:22
2

ImportError: No module named 'Tkinter' In Python 3 Tkinter is changed to tkinter Try import tkinter as tk

Hope it helps!

Ayush Raj
  • 294
  • 3
  • 14