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?
Related
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.
I have a bunch of images in "test" folder and I have a python code in a file to display those images. Here is the code
import torch
from IPython.display import Image, clear_output
import matplotlib
import glob
import PIL
from IPython.display import Image, display
from io import BytesIO
print("hi")
for imageName in glob.glob('test/images/*.jpg'): #assuming JPG
print("in \n")
display(Image(filename=imageName)) #displaying successfully in colab
print("out")
print("\n")
and here is the output when I ran the python file in ubuntu terminal.
I have no other tab opened to display image the image. I tried other answers in stack overflow but didn't work. However, the images are visible in colab's output.
You are running in a terminal, terminals can only display text.
Thus display(Image(...)) cannot display your image.
display and Image are really IPython/Jupyter centric utilities that cannot completely function when using purely Python.
If you want to show images when running in a terminal you will need to use something like pillow or matplotlib and ask them to open a new window with the image, and depending on how you do it, you may need to close the window for the program to keep executing.
You can also locally run jupyter notebook (or, jupyter lab) to open a web interface that should support images.
When looking at a solution to import an IPython file from another, I found the solution of:
import import_ipynb
from Genetic_Algorithm_Library_Code import ga
However, when I run the code, the program from the other file starts running immediately. Is it possible to only import functions and run them when I want on the second file?
Edit:
Problem solved: Used nbimporter instead.
import nbimporter
import gapro as gapro
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
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!!!