1

Short Version:

I have a series of python scripts that connect together (one .py closes and runs a separate .py). This works completely fine when running it through the terminal in VS Code or cmd line. Once it is in a .exe by pyinstaller, only the first code works and the program closes once it tries to execute a separate .py file.

Details:

All of the separate python files are saved in the same directory. The first one to open, 'Main.py', has a tkinter interface that allows the user to select which .py script they want to run. The code then closes the Main window and opens the selected python script using exec(open('chosen .py').read()). (This is a simplified version of the initial code but I am having the same issues)

import tkinter as tk
from tkinter import ttk
from tkinter.constants import W
from tkinter import messagebox as mb

""" Open a window to select which separate script to run"""

root = tk.Tk()
root.title('Selection Window')
root.geometry('300x200')

frame_1 = tk.LabelFrame(root, text='Choose Program')
frame_1.pack()

# Using this function to update on radio button select
def radio_button_get():
    global program_int
    choice = radio_ID.get()
    if(choice == 1):
        program_int = 1
    elif(choice == 2):
        program_int = 2

# Display confirmation popup
def run_script():
    if(program_int == 1):
        select = mb.askokcancel("Confirm", "Run choice 1?")
        if(select == 1):
            root.destroy()
        else:
            return
    if(program_int == 2):
        select = mb.askokcancel("Confirm", "No selection")
        if(select == 1):
            root.destroy()
        else:
            return

# Create radio buttons to select program 
radio_ID = tk.IntVar()
radio_ID.set(2)
program_int = 2     # Set default selection

choice_1 = tk.Radiobutton(frame_1, text='Execute Script 1', variable=radio_ID, value=1, command=radio_button_get)
choice_1.pack()
no_choice = tk.Radiobutton(frame_1, text='No Selection', variable=radio_ID, value=2, command=radio_button_get)
no_choice.pack()

# Button to run the selected code
run_button = ttk.Button(root, text='Run', command=run_script)
run_button.pack()
root.mainloop()

# Execute the other python script 
if(program_int == 1):
    exec(open('Script1.py').read())

The next code is the 'Script1.py' file which 'Main.py' runs at the end. This is the step which works fine in VS Code and cmd line, but causes the .exe from pyinstaller to close.

import tkinter as tk
from tkinter import ttk

""" Create this programs GUI window"""

root = tk.Tk()
root.title('Script 1')

def run():
    root.destroy()

label = ttk.Label(root, text='Close to run')
label.pack()
button = ttk.Button(root, text='Close', command=run)
button.pack()

root.mainloop()

""" Do some code stuff here"""

# When above code is done, want to return to the Main.py window
exec(open('Main.py').read())

Each independent .py file have been successfully turned into .exe files with pyinstaller previously. The cmd line command that I am using to execute pyinstaller is pyinstaller 'Main.py' This successfully creates a Main.exe in the dist folder and also includes a build folder.

I have read through pyinstallers documentation, but have not found anything that I believe would be useful in this case. The nearest issue I could find was importing python scripts as modules in the .spec file options but since the code executes the python script as a separate entity, I don't think this is the fix.

Would the issue be in how the scripts are coded and referencing each other, or with the installation process with pyinstaller? If I missed something in the documentation that would explain this issue, please let me know and I will look there!

Any help is greatly appreciated, thank you

Tyler B
  • 62
  • 10
  • Probably `Script1.py` is not found, the path to the executable is not where the files are extracted to. Have a look at: https://pyinstaller.readthedocs.io/en/stable/runtime-information.html – Maurice Meyer Jun 30 '21 at 21:05
  • 1
    Try replacing `exec(open('Script1.py').read())` by `import Script1`. – acw1668 Jul 01 '21 at 02:23

2 Answers2

0

We must avoid using the .exec command. It is hacky but unsafe. Ref: Running a Python script from another

Instead use import :

# Execute the other python script 
if(program_int == 1):
    import Script1

And here too:

# When above code is done, want to return to the Main.py window
import Main

That's it, now use pyinstaller.


EDIT:

Why .exe file fails to execute another script, and why exec() is the problem:

According to the documentation:

Pyinstaller analyzes your code to discover every other module and library your script needs in order to execute. Then it collects copies of all those files – including the active Python interpreter! – and puts them with your script in a single folder, or optionally in a single executable file.

So, when pyinstaller is analyzing & creating the .exe file, it only executes the exec() function that time (so no error thrown while pyinstaller runs), pyinstaller does not import it or copies it to your .exe. file, and then after the .exe file is created, upon running it throws error that no such script file exists because it was never compiled into that .exe file.

Thus, using import will actually import the script as module, when pyinstaller is executed, and now your .exe file will give no error.

Kartikeya
  • 818
  • 2
  • 6
  • 11
  • This solved my original issue, so thank you! However, now once going to script1 and coming back to main, if I try to select the option to go to script1 again it stops running. Is there a workaround for this? Perhaps check, at the beginning of Main.py, to see if Script1 is imported already, and if it is somehow 'un-import it'? – Tyler B Jul 01 '21 at 13:16
  • Yes, this is happening because `import` actually imports only once, it does execute the script again and again like a function. So, a solution I can think of is- import Script1 as a function. Wait, can't explain in comments, adding a new answer. – Kartikeya Jul 01 '21 at 13:54
  • I found a workaround by first checking `if 'Script1' in sys.modules: sys.modules.pop('Script1')` . However, I really like the method you put in the new answer you posted and I will experiment with structuring my code that way instead. If it works, ill change the accepted answer to the second one! Thanks – Tyler B Jul 01 '21 at 14:47
  • Yes, using `sys.modules` is really an efficient way to work in `pyinstaller`. No problem, go with it. In Python, one problem may have many solutions. – Kartikeya Jul 01 '21 at 17:42
0

Instead of importing the scripts as modules, for them to be re-executed again and again, import another script as a function in Main.py

Also, instead of destroying your Main root window (since you won't be able to open it again unless you create a new window), use .withdraw() to hide it and then .deiconify() to show.


First, in Script1.py:

import tkinter as tk
from tkinter import ttk

""" Create this programs GUI window"""
def script1Function(root):      #the main root window is recieved as parameter, since this function is not inside the scope of Main.py's root
    root2 = tk.Tk()             #change the name to root2 to remove any ambiguity
    root2.title('Script 1')

    def run():
        root2.destroy()         #destroy this root2 window
        root.deiconify()        #show the hidden Main root window
    label = ttk.Label(root2, text='Close to run')
    label.pack()
    button = ttk.Button(root2, text='Close', command=run)
    button.pack()

    root2.mainloop()

Then, in Main.py:

import tkinter as tk
from tkinter import ttk
from tkinter.constants import W
from tkinter import messagebox as mb
from Script1 import script1Function         #importing Script1's function


# Execute the other python script
def openScript1():    
    root.withdraw()                         #hide this root window
    script1Function(root)                   #pass root window as parameter, so that Script1 can show root again
    

""" Open a window to select which separate script to run"""

root = tk.Tk()
root.title('Selection Window')
root.geometry('300x200')

frame_1 = tk.LabelFrame(root, text='Choose Program')
frame_1.pack()

# Using this function to update on radio button select
def radio_button_get():
    global program_int
    choice = radio_ID.get()
    if(choice == 1):
        program_int = 1
    elif(choice == 2):
        program_int = 2

# Display confirmation popup
def run_script():
    global program_int                      #you forgot to make it global
    if(program_int == 1):
        select = mb.askokcancel("Confirm", "Run choice 1?")
        if(select == 1):
            openScript1()
        else:
            return
    if(program_int == 2):
        select = mb.askokcancel("Confirm", "No selection")
        if(select == 1):
            root.destroy()
        else:
            return

# Create radio buttons to select program 
radio_ID = tk.IntVar()
radio_ID.set(2)
program_int = 2     # Set default selection

choice_1 = tk.Radiobutton(frame_1, text='Execute Script 1', variable=radio_ID, value=1, command=radio_button_get)
choice_1.pack()
no_choice = tk.Radiobutton(frame_1, text='No Selection', variable=radio_ID, value=2, command=radio_button_get)
no_choice.pack()

# Button to run the selected code
run_button = ttk.Button(root, text='Run', command=run_script)
run_button.pack()
root.mainloop()
Kartikeya
  • 818
  • 2
  • 6
  • 11