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

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.

Related

Converting python to exe gives an exe with black screen

I have made a python app in one python file with the following import statements:
import numpy as np
import PySimpleGUI as sg
import cv2
from PIL import Image, ImageTk
import sys
import os
import pydicom as dicom
from io import BytesIO
import matplotlib.pyplot as plt
from pydicom.pixel_data_handlers import convert_color_space
from scipy.optimize import curve_fit
When I run my app (in spyder), it gives simply a window telling the user to select a folder:
When the user chooses a folder and click ok, the app starts to open other window with buttons to click on etc. I have tested in spyder many times that my code does not contain error and in particular will not crush. My goal is to convert this one single python file into an exe such that the user can just double click on exe (without python environment) such that the code starts to run and they should see the window telling them to select folders immediately after double click.
I use pyinstaller for the task (with command pyinstaller --onefile main.py, where main.py is the my file. The process is slow (and produces a lot of text) but there does not seem to have errors. When I click on the final exe file, I get two empty windows:
What can there possibly go wrong? Is there any workaround to this? That is, is there anything other than pyinstaller to help me with the task?

When importing pyautogui running the code breaks the script?

When I run the code through an extension in Visual Studio Code, it runs fine. But the real problem is when I open the program without the extension it errors and closes instantly. I traced the error back to this line right here.
import os
import pyautogui #Line Right here
from time import sleep
Having the import pyautogui breaks the whole script, and I really do not know why?
This closes out when I import pyautogui

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

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