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

8
  • 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. Commented Mar 9, 2021 at 13:10
  • @AST interesting! yes each "click" is creating new window of this program Commented Mar 9, 2021 at 13:38
  • I see, could you please show what does the class A do or make a minimal, reproducible example to try? Commented Mar 9, 2021 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? Commented Mar 9, 2021 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. Commented Mar 9, 2021 at 14:30

1 Answer 1

0

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.