Thank you - that is helpful. I'm sure it's not recommended, but I can get everything to work now if I replace sys.executable in the code with the path to my python.exe in quotes.
Matthew On Tuesday, June 22, 2021 at 11:40:58 PM UTC-7 [email protected] wrote: > A couple of things occur to me on looking at your code: > > > > 1. You have not if __name__ == "__main__": in your file - this is a > great pattern to follow. > 2. You would probably be better off using the multiprocessing library > rather than subprocess > > When you code: self.process = subprocess.Popen([sys.executable, "-u", > "testsubprocess.py"], shell=True, stdout=subprocess.PIPE, > stdin=subprocess.PIPE, stderr=subprocess.STDOUT) is executed directly as > python > simpleGUI.py then sys.executable will be /path/to/python.exe but when you > execute your simpleGUI.exe then it will be simpleGUI.exe this is why you > get a new instance! > > > > Hope that helps! > > > > Steve > > > > *From:* [email protected] <[email protected]> *On Behalf > Of *Matthew Lawrence > *Sent:* 22 June 2021 17:54 > *To:* PyInstaller <[email protected]> > *Subject:* [PyInstaller] subprocess not working after onefile pyinstaller > even with stdout/stdin defined > > > > Hi - I posted this question to Stack Overflow ( > https://stackoverflow.com/questions/68042440/subprocess-not-working-after-onefile-pyinstaller-even-with-stdout-stdin-defined), > > but haven't had a response, so thought I'd try here. Apologies if this is > not the right forum. > > I have a tkinter GUI with a text box and run button. Pressing the run > button turns it to yellow and starts a subroutine that prints a few > numbers. Text output from the subroutine is redirected to the GUI text box. > However, after creating a standalone executable file with pyinstaller, it > no longer works. Pressing the run button doesn't seem to start the > subprocess. It does turn yellow, but no text appears in the text box and it > seems to start another instance of the main program - a second GUI appears > after about 10 seconds which is how long it takes for the initial GUI to > appear. The run button stays yellow on the initial GUI. > > I've seen a bit online about other people having issues with subprocesses > not running after pyinstaller, but most of the solutions seem to be to make > sure stdout, stdin are set to subprocess.PIPE which I have, so I'm at a bit > of a loss what to try next. > > When running the resulting executable from Anaconda prompt, I also get no > error messages or anything useful to help debug. > > I'm creating my standalone with this: > > pyinstaller --onefile --add-data "testsubprocess.py;." simpleGUI.py > > > > I've also tried without --add-data "testsubprocess.py;.", making sure my > executable and testsubprocess.py are in the same folder. > > My subprocess file, testsubprocess.py is: > > import time > > > > for i in range(3): > > print("%d.%d" % divmod(i, 10)) > > time.sleep(0.5) > > > > My main GUI file, simpleGUI.py, is: > > import sys > > import subprocess > > from threading import Thread > > import tkinter as tk > > from queue import Queue, Empty > > > > > > def iter_except(function, exception): > > try: > > while True: > > yield function() > > except exception: > > return > > > > > > class DisplaySubprocessOutputDemo: > > def __init__(self, root): > > self.root = root > > > > width=600 > > height=350 > > xloc=0 > > yloc=10 > > self.root.geometry('%dx%d+%d+%d' % (width, height, xloc, yloc)) > > > > self.statustext = tk.Text(self.root, height=4, width=30) > > self.statustext.grid(row=3, column=1) > > > > self.startbutton = tk.Button(self.root, text = 'Start', > command=self.startprocess, bg='green', activebackground = 'orange') > > self.startbutton.config(height = 2, width = 15) > > self.startbutton.grid(row = 5, column=0,sticky='E') > > self.startbuttonpresses = 0 > > > > exitbutton = tk.Button(self.root, text = 'Exit', > command=self.quit, bg='red') > > exitbutton.config(height = 2, width = 15) > > exitbutton.grid(row = 5, column=4, sticky='E') > > > > > > def startprocess(self): > > self.startbuttonpresses = self.startbuttonpresses+1 > > > > if self.startbuttonpresses == 1: > > > > self.startbutton.configure(bg='yellow') > > self.startbutton.configure(text='Stop') > > > > self.process = subprocess.Popen([sys.executable, "-u", > "testsubprocess.py"], shell=True, stdout=subprocess.PIPE, > stdin=subprocess.PIPE, stderr=subprocess.STDOUT) > > > > q = Queue(maxsize=1024) > > t = Thread(target=self.reader_thread, args=[q]) > > t.daemon = True > > t.start() > > > > self.updatetext(q) > > > > else: > > self.startbuttonpresses = 0 > > self.process.kill() > > self.startbutton.configure(bg='green') > > self.startbutton.configure(text='Start') > > > > > > def reader_thread(self, q): > > try: > > with self.process.stdout as pipe: > > for line in iter(pipe.readline, b''): > > q.put(line) > > finally: > > q.put(None) > > > > > > def updatetext(self, q): > > for line in iter_except(q.get_nowait, Empty): # display all content > > if line is None: > > self.startbuttonpresses = 0 > > self.startbutton.configure(bg='green') > > self.startbutton.configure(text='Start') > > > > return > > else: > > self.statustext.insert(tk.END, line) > > > > self.root.after(400, self.updatetext, q) > > > > > > def quit(self): > > try: > > self.process.kill() > > except Exception: > > pass > > self.root.destroy() > > > > > > root = tk.Tk() > > app = DisplaySubprocessOutputDemo(root) > > root.protocol("WM_DELETE_WINDOW", app.quit) > > root.eval('tk::PlaceWindow %s center' % > root.winfo_pathname(root.winfo_id())) > > root.mainloop() > > -- > You received this message because you are subscribed to the Google Groups > "PyInstaller" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/pyinstaller/127a328b-e2c9-4d08-8a71-f574b0c18420n%40googlegroups.com > > <https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fpyinstaller%2F127a328b-e2c9-4d08-8a71-f574b0c18420n%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=04%7C01%7C%7C08e0fa1774c0498fddcd08d935c00f0f%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637599921260094389%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=Jbo9jd%2BiqD1JsTMih5OrxdsF7ZejMazTRj0Tx%2Bsp244%3D&reserved=0> > . > -- You received this message because you are subscribed to the Google Groups "PyInstaller" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/77c2276a-8850-406e-bc7d-75dfdbf75ed6n%40googlegroups.com.
