PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] How to capture the output of pyistaller.__main__.run

 August 18, 2022     output, pyinstaller, python-3.x     No comments   

Issue

I came to a point where I just wanted to make an external programm to make .exe's from Pyhton files via PyInstaller.__main__.run with just selecting a few files and ticking some boxes. But I still wanted to have a look at the process' outputs. And since they are displayed red in the Python-IDEs shell I thought they were (cropped to one line) error outputs, so I tried to override sys.stderr but that did not work out at all. But since they are neither stdoutputs to get via sys.stdout I don't know how to get them. So I thought of two possible ways to solve this but don't know how to do each of them and did not found anything on my google research:

a) Someone here knows a solution how to get those outputs (appreciated)

OR

b) I need an option to control wether or not the shell output window is in the back- or foreground (or iconified/deiconified).

Thanks in advance.


Solution

So since it seems no one had an answer I want to post the solution I've found out about:

import subprocess
from tkinter import *
from threading import Thread

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.rowconfigure(0,weight=1)
        self.columnconfigure(0,weight=1)
        self.text=Text(self)
        self.text.grid(row=0,column=0,sticky=NSEW)
        sby=Scrollbar(self,command=self.text.yview)
        sby.grid(row=0,column=1,sticky=NS)
        sbx=Scrollbar(self,command=self.text.xview,orient=HORIZONTAL)
        sbx.grid(row=1,column=0,sticky=EW)
        self.text.config(yscrollcommand=sby.set,xscrollcommand=sbx.set)
        Button(self,text='Start',command=self.callback).grid(row=2,column=0,columnspan=2,pady=5)
        #creating a tkinter window with text widget to display the outputstream and start up button

        self.thread=Thread(target=self.update_)
        self.thread.deamon=True
        #creating Thread

        self.mainloop()
    def callback(self):
        sui=subprocess.STARTUPINFO()
        sui.dwFlags|=subprocess.STARTF_USESHOWWINDOW
        #setting subprocess startupflags so that ther won't be an extra shell prompt
        self.prc=subprocess.Popen(('~/Python/Scripts/pyinstaller.exe', ... , '~/myprg.py'),
                                  stdout=subprocess.PIPE,stderr=subprocess.PIPE,startupinfo=sui)
        #starting subprocess with redirected stdout and stderr
        self.thread.start()
    def update_(self):
        try:
            with self.prc.stderr as pipe:
                for line in iter(pipe.readline,b''): self.text.insert(END,line)
                #inserting stderr output stream as is comes
        finally: pass

if __name__=='__main__':
    App()


Answered By - Nummer_42O
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing