0

I have a Python script that contains the following modules:

from tkinter import *
from tkinter import ttk
from tkinter import filedialog

When I run the code in IDLE by pressing F5 the script runs fine and starts my app.

However, when I go to the command prompt and type

python ScannerApp.py

I get the following error:

File "tkinterTest.py", line 1, in <module>
from tkinter import *
ImportError: No module named tkinter

How do I get rid of this error? The ultimate goal being to make this script into a .exe.

One thought is that python is not added to my environmental variables under Path, it is added as it's own variable. Could that be causing the issue?

My question does not pertain to the difference between Tkinter and tkinter. My question was about why when I ran code through the command line I was getting an error. The issue happened to be that my environmental variable python was set to run python 2.7 instead of the necessary python 3.6 (which uses tkinter).

M Waz
  • 755
  • 1
  • 7
  • 18

3 Answers3

0

Try adding this for cross compatibility instead of your previous import code.: (Hoping that's the problem)

try:
    from tkinter import *
    from tkinter import ttk,filedialog
except:
    from Tkinter import *
    from Tkinter import ttk,filedialog
Ubdus Samad
  • 1,218
  • 1
  • 15
  • 27
  • Cross compatibility is the issue, but this solution didn't fix my problem. I was able to solve by changing the environmental variable python to run 3.6 instead of 2.7. – M Waz Mar 06 '18 at 15:54
  • That's really a bad idea, and I don't see any flaw in my code, it should work perfectly. – Ubdus Samad Mar 06 '18 at 16:06
  • It could be that this `ttk` might not be compatible or present in Python 2, else it seems fine to me. – Ubdus Samad Mar 06 '18 at 16:08
  • I can tell you that it doesn't. My solution fixed my issue and as this is an app that will only be used on one computer, I do not see why I would need to account for a python version I will not be using. – M Waz Mar 06 '18 at 16:08
  • Could you paste the error you're getting? and it's not a standard practice to change environment variable defaults just for one app, but if you're aware of the consequences and know that it's no harm doing it, then it's fine. – Ubdus Samad Mar 06 '18 at 16:11
  • 2
    The issue with the code above is that in [tag:python-2.x] neither [tag:ttk] nor `fildialog` are submodules to [tag:tkinter]. Instead there are _separate_ `ttk` and `tkFileDialog` modules which should effectively be the same to those of tkinter for [tag:python-3.x]. TL;DR: Replace `from Tkinter import ttk,filedialog` with `import ttk; import tkFileDialog as filedialog` to have the desired result. – Nae Mar 06 '18 at 16:26
-1

The solution to my problem was to change the environmental variable python to run version 3.6 instead of 2.7. The issue was a cross compatibility issue and I found it easier to change the variable instead of trying to have it try both Tkinter and tkinter modules depending on the particular version.

M Waz
  • 755
  • 1
  • 7
  • 18
  • In my opinion, this solution is a solution to my particular problem. I explained what my problem was and if someone has an identical issue, this will be a solution. I never claim this to be a solution for every tkinter or Tkinter issue. – M Waz Mar 06 '18 at 16:25
  • That's not really how noobs work you know, I've been a noob and I know that I would've smashed that environment variable without thinking twice if yours have been an accepted answer. – Ubdus Samad Mar 06 '18 at 16:33
  • I only ever use python 3.6. Again, this solution works for me. It is not my issue that you do not like it. Keep your down vote. – M Waz Mar 06 '18 at 16:36
-2

Your issue might be that python3 doesn't use Tkinter (with a capital T) but tkinter. That is if you're using pyhton3 of course ^^

https://stackoverflow.com/a/17843652/9368855

Blustch
  • 1
  • 3
  • I don't think this is the issue because when I run the script through IDLE it works. This issue only occurs when I run the script through the command line. – M Waz Mar 06 '18 at 15:45
  • You have it backwards if I'm not mistaken, and this provides an explanation but not a solution. – Mad Physicist Mar 06 '18 at 15:46
  • Also, if the answer is a direct link, vote / flag to close as a duplicate instead of providing a sub-par answer. – Mad Physicist Mar 06 '18 at 15:48