Running Python file from command line does not load modules - python

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 Tkinter (with a capital T) but tkinter. That is if you're using pyhton3 of course ^^
https://stackoverflow.com/a/17843652/9368855

Related

auto-py-to-exe created file does not execute properly

I used auto-py-to-exe to convert my .py to a .exe and why my py works as expected, my exe does not work. The console just springs up for a split second, before closing again and the code does not execute.
In case anyone wonders, these are the modules used in the code:
import os
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askdirectory
import cv2
import numpy as np
I do not understand where the error occurs...
EDIT:
"Open cmq and run the .exe on the terminal. In this way you can see the error message"
The Console throws no error, it looks like everything worked properly, but it clearly didn`t.

Python 3.8.9 MacOS M1 tkinter UI not opening inside of VSCode

so I am trying to use Tkinter inside of VSCode on my M1 mac. When I use Tkinter through IDLE it works fine but once I try to open it inside of VSCode it the UI submenu just stalls. I'm using python 3.8.9 as my kernel but it won't seem to open even when I switch to any other versions. Tkinter is installed fine it is just some issue with how VSCode is running it but I have no idea how to fix it.
import tkinter as tk
window = tk.Tk()
Here I provided some screenshots of what happens
While using Idle
While using VSCode (The app just bounces up and down never opening the UI)
You have not started the event loop. IDLE does mainloop for you, otherwise, you need to call it by yourself.
import tkinter as tk
root = tk.Tk()
root.mainloop()
You can refer to here.

Python quits whenever tkinter is called

Using python 3.6 on macOS 10.12 I cannot get any function out of tkinter.
It imports just fine but then calling Tk() or filedialog() immediately causes this error Window of:
Python quit unexpectedly
I updated Tcl/Tk from ActiveState and this appeared to have no effect.
I have used tkinter once for a class but I don't have a lot of experience with it, am I simply calling the wrong functions? It seems like any function from the tkinter library causes the same problem.
from tkinter import *
Tk()
OR
filedialog.askopenfilename()

Tkinter NameError only when running script from shell

I've got a Tkinter Python program, a reduced version of which can be found below:
from tkinter import *
from tkinter.ttk import *
filedialog.askopenfilename()
When I run this script from IDLE, I do not get any errors.
However, when run from PowerShell, using python myscript.py I get
NameError: could not find name 'filedialog'
Windows 10 x64 on a mid-2012 MacBook Pro
IDLE is probably importing it already, but in general since filedialog is a tkinter module it won't get imported with the bare:
from tkinter import *
Include an extra:
from tkinter import filedialog
and you should be good to go.

Python Tkinter - .py file doesn't work when double clicked

I am running Windows 7 and have Python 3.3 64 bit installed. I seem to have a problem importing the tkinter module, I can import it fine through the python IDLE and it will work, but when I save the .py file and double click it, a cmd window will open and say:
Traceback (most recent call last):
File "C:Users\username\Desktop\g.py", line 3, in <module>
from tkinter import *
ImportError: No module named tkinter
I have tried the following:
I have tried import tkinter, from tkinter import *, and import tkinter as tk and they don't seem to work when the .py file is opened directly (double clicked).
I also double checked the path variable and it was set correctly.
I uninstalled python and reinstalled it.
I checked to see if tkinter is in the folder C:\Python33\Lib\, and it is.
I do have a mainloop() in my program.
In my program, tkinter is all lowercase.
I tried a lot of solutions online from other posts, and they didn't work for me.
The top of my code is:
import sys
from tkinter import *
I don't know what I am missing, any suggestions?
I'm going to make this an answer then for anybody in the future.
The problem is that Windows is currently set to run all .py files with a different executable (probably a Python 2.x one) To fix the problem, follow these steps:
Right-click a .py file.
In the menu that pops up, go to Open with.
In the submenu that pops up, click on Choose default program...
A window will then appear. In this window, click on the Browse... button.
Then, go find the Python execuatble. It should be at C:\Python33\python3.3.exe. (There might be multiple pythonX.exe files. If one doesn't work, try another.)
Once you select it, click Open.
If done correctly, this procedure will manually reset the default executable for .py files to be the Python 3.x one. Meaning, your script should run fine now.
This is actually a simple solution.
You have:
from tkinter import *
You need:
from Tkinter import *
Capitalization is very specific!!!

Categories

Resources