Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python .exe not working properly

I downloaded pyinstaller in order to turn my python scripts into an executable program. I have a main script ((homepage.py) that, when ran, it would display a window with two buttons (button1 and button2). Each of the two buttons, if clicked, would run its corresponding python script (input1.py or input2.py) that displays a window where the user inputs data to be graphed in excel.

My scripts work correctly when I run them in Spyder through Anaconda. However, when I turn my main script (Homepage.py) into an executable program and run it, the main window with the two buttons appear, but when I click any of the two buttons, the corresponding window does not appear and the program closes. Does anyone know why that would happen? Do I need to turn my input1.py and input2.py into .exe programs as well like I did with homepage.py?

In my scripts, when I need click any of the buttons, I would hide the main window and show the new window. Would hiding and showing be what is causing my program to not function correctly even if it is working in Spyder?

main window (homapge.py) imports and class name,

from PyQt5 import QtCore, QtGui, QtWidgets
from input1 import Ui_input1
from input2 import Ui_input2

class Ui_homepage(object):

button1 (input1.py) imports and class name,

from PyQt5 import QtCore, QtGui, QtWidgets
import csv, os, subprocess, output1

class Ui_input1(object):

button2 (input2.py) imports and class name,

from PyQt5 import QtCore, QtGui, QtWidgets
import csv, os, subprocess, output2

class Ui_input2(object):

Also, inside both of input1.py and input2.py, I have a button function that returns to the main window (homepage.py) when clicked, and inside that function I import,

from homepage import Ui_homepage

At the end of homepage.py, input1.py, and input2.py I have this import,

if __name__ == "__main__":
    import sys

Let me know if you need any more clarification or code.

EDIT 1:

The code that I use in the main window (homepage.py) that calls button1,

self.button1.clicked.connect(self.displayWindow1)

def displayWindow1(self):
    self.window = QtWidgets.QMainWindow()
    self.uiInput1 = Ui_input1()
    self.uiInput1.setupUi(self.window)
    self.homepage.hide()
    self.window.showMaximized()

The code that I use in the main window (homepage.py) that calls button2,

self.button2.clicked.connect(self.displayWindow2)

def displayWindow1(self):
    self.window = QtWidgets.QMainWindow()
    self.uiInput2 = Ui_input1()
    self.uiInput2.setupUi(self.window)
    self.homepage.hide()
    self.window.showMaximized()

EDIT 2:

I tried running the program after I changed,

from input1 import Ui_input1
from input2 import Ui_input2

to,

import input1 
import input2

I got this error in the command prompt window after typing pyinstaller --onefile --windowed homepage.exe,

25160 WARNING: lib not found: MSVCR90.dll dependency of c:\users\bj914e
\appdata\local\continuum\anaconda3\Library\bin\zlib.dll

and I got these errors in a warnhomepage.txt file,

missing module named resource - imported by posix, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named posix - imported by os, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named _posixsubprocess - imported by subprocess, C:\Users
\bj914e\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named org - imported by pickle, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named readline - imported by cmd, code, pdb, C:\Users
\bj914e\Desktop\DAMS\DAMS_Version_4\homepage.py
excluded module named _frozen_importlib - imported by importlib, 
importlib.abc, C:\Users\bj914e\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named _frozen_importlib_external - imported by 
importlib._bootstrap, importlib, importlib.abc, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named _winreg - imported by platform, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named _scproxy - imported by urllib.request
missing module named java - imported by platform, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named 'java.lang' - imported by platform, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py, xml.sax._exceptions
missing module named vms_lib - imported by platform, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named termios - imported by tty, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py, getpass
missing module named grp - imported by shutil, tarfile, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named pwd - imported by posixpath, shutil, tarfile, 
http.server, webbrowser, C:\Users\bj914e\Desktop\DAMS\DAMS_Version_4
\homepage.py, netrc, getpass
missing module named _dummy_threading - imported by dummy_threading, 
C:\Users\bj914e\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named 'org.python' - imported by copy, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py, xml.sax
missing module named cPickle - imported by xlsxwriter.compat_collections
missing module named cStringIO - imported by cPickle
missing module named copy_reg - imported by cPickle, cStringIO
missing module named StringIO - imported by xlsxwriter.compatibility

UPDATE:

I was able to fix my problem. It turned out to be a very simple solution. After I clicked one of the buttons the program closed, but a command prompt window appeared and disappeared within a millisecond that I did not notice before until it was mentioned to me in the solution below. The solution was moving all of the data files that input1.py and input2.py use into the dist file with the .exe program.

P.S: some recording programs are too slow to record the window that appears and disappears, so I tried with my smartphone (iPhone) camera and it was able to detect it.

like image 339
prexos Avatar asked Jul 10 '18 13:07

prexos


People also ask

Why does the Python exe file only flashes a DOS window and disappears?

The fact that it works from default folder but not after being "pasted to desktop" suggests that your script has some dependencies/imports that can not be found when being run from desktop. You have to convert your python script to exe. Use py2exe modul to do that.

Do you need Python to run a Python exe?

py2exe is a Python extension which converts Python scripts (. py) into Microsoft Windows executables (.exe). These executables can run on a system without Python installed.

Can we create exe file in Python?

The first (auto-py-to-exe) has a friendly interface that will help beginners to easily create executables, while the second (PyInstaller) offers a straightforward way to create executables through the terminal.


1 Answers

I had a very similar problem, the issue was a missing module(s). Your exe may use other modules that pyinstaller did not detect. For me, as soon as I imported Decimal module my exe worked like a charm! When your homepage.exe closes/crashes, it tells you which module is missing. You will only have a millisecond to see it, I recorded my screen and slowed down the footage to see the error message. Unconventional, perhaps, but worked for me!

like image 189
KMoe Avatar answered Sep 26 '22 02:09

KMoe