1

part of code:

class A:
    def __init__(self,my_arg):

      do sth...

    def f1(self):

       one = (sys.executable, "script.py")
       output = subprocess.Popen(one, stdout=subprocess.PIPE)

    def f2(self):

       do_sth...

class program(tk.Tk):
    def __init__(self):
        super().__init__()
        ...
        
        button_run  = Button(root)
        button_run.configure(command = self.run_program)
        root.mainloop()

    def run_program(self): 
        file_list = ['file1','file2']
        for file in file_list:
           cls_A = A(file)
           cls_A.f1()
           cls_A.f2()

if __name__== '__main__':
   program()

Problem:

I have read many topics about this problem but have not seen any solution. subprocess is blocking Tkinter after conversion to exe, and my program is not running same as from script, after clicking RUN button is creating new tkinter window and again...

I know that this problem is connected with subprocess part, how to run external module same as subprocess do , but diferently?

this solution not working: pyInstaller loads script multiple times

sygneto
  • 1,761
  • 1
  • 13
  • 26
  • Does the main window respawn or is a new window created? Also, have you used `multiprocessing` within the mainloop? Because that would produce a new window once again for the different process. – astqx Mar 09 '21 at 13:10
  • @AST interesting! yes each "click" is creating new window of this program – sygneto Mar 09 '21 at 13:38
  • I see, could you please show what does the class `A` do or make a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to try? – astqx Mar 09 '21 at 13:58
  • @AST i am trying to 're produce' example but its hard, its big program and i cannot paste all i here, but could you tell me how to use multiprocessing here? – sygneto Mar 09 '21 at 14:24
  • 1
    Hmm, I meant that the problem could occur if you use `multiprocessing`, you don't have to use that. At least try to mention what kind of operation does it perform. Nothing seems to be suspicious in the current code, except the calls to functions in class `A`. – astqx Mar 09 '21 at 14:30
  • @AST i will try to produce example code soon, but interesting thing is that when I am creating exe file with no options in pyinstaller i have problem with duplicated windows(creating of new each time i click button) when i am using `-w -F` program works but gives empty output – sygneto Mar 09 '21 at 14:35
  • That's interesting, I don't recall a case of that happening as of now, add a tag of `pyinstaller` to your question, could be possible for someone else in that community has come across something like this before. And as far the "empty output" is concerned the `-w` flag creates the exe in windowed mode without a console, so if your program gave a console output, you would not be able to see that. – astqx Mar 09 '21 at 14:45
  • @AST Ive change my question, take a look. problem is caused by subprocess, maybe somehow I can replace it? – sygneto Mar 15 '21 at 14:12

1 Answers1

1

The problem arises due to this line

one = (sys.executable, "script.py")

This has been mentioned in the documentation here

When a normal Python script runs, sys.executable is the path to the program that was executed, namely, the Python interpreter. In a frozen app, sys.executable is also the path to the program that was executed, but that is not Python; it is the bootloader in either the one-file app or the executable in the one-folder app.

Due to this, your exe is relaunched and the script doesn't get executed.

You could try importing the script as a module using import script, and use OOP to simplify the execution. If the script is blocking, then you can use threading to launch it in a different thread and if the script needs to executed in a separate main thread, then you can create another executable for your script and call that (if using the --onedir mode, you can create a python file that imports the dependencies for both the main and the other script and builds a base folder, you can later create 2 exes in the same mode and place them in the common base directory to have relatively lesser size overall).

astqx
  • 2,058
  • 1
  • 10
  • 21