简体   繁体   中英

Running Python file from command line does not load modules

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

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM