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).

8
  • 1
    Maybe the idle and command line are different environments Commented Mar 6, 2018 at 15:45
  • How do I get them to be the same? Commented Mar 6, 2018 at 15:45
  • Are you using Windows? Commented Mar 6, 2018 at 15:46
  • Yes, I am using windows. Commented Mar 6, 2018 at 15:47
  • 2
    The issue is a cross compatibility issue. My command line variable python was using python 2.7 while it should've been using python 3.6. Changing the environmental variable to python 3.6 solved my issue. Thank you Commented Mar 6, 2018 at 15:53

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

6 Comments

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.
That's really a bad idea, and I don't see any flaw in my code, it should work perfectly.
It could be that this ttk might not be compatible or present in Python 2, else it seems fine to me.
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.
The issue with the code above is that in python-2.x neither ttk nor fildialog are submodules to tkinter. Instead there are separate ttk and tkFileDialog modules which should effectively be the same to those of tkinter for python-3.x. TL;DR: Replace from Tkinter import ttk,filedialog with import ttk; import tkFileDialog as filedialog to have the desired result.
|
-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.

3 Comments

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.
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.
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.
-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

3 Comments

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.
You have it backwards if I'm not mistaken, and this provides an explanation but not a solution.
Also, if the answer is a direct link, vote / flag to close as a duplicate instead of providing a sub-par answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.