Search code examples
pythonpyinstallerexecutable

.exe file not working starting from .py file


I am new to programming. I created a very simple python script for moving the mouse every couple minutes (the code will follow) and it is working properly. Once created the .exe file of the .py script using pyinstaller, the .exe doesn't work anymore: a console opens and just does nothing. Is there anything wrong or that I am missing?

import time, pyautogui
import PySimpleGUI as sg
import multiprocessing


def KeepUI():
    
    sg.theme('Dark')
    layout = [
        [sg.Text('Keep-Me-Up is now running.\nClose it to disable it.')]
    ]
    window = sg.Window('Keep-Me-Up', layout)
    
    p2 = multiprocessing.Process(target = dontsleep)
    p2.start()
    
    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED:
            if p2.is_alive(): 
                p2.terminate()
            break

def dontsleep():
    while True:
        for i in range(0,100):
            pyautogui.moveTo(1000,i*10)
            time.sleep(120)



if __name__ == '__main__':
    p1 = multiprocessing.Process(target = KeepUI)
    p1.start()

I tried creating the .exe file using pyinstaller (pyinstaller --onefile filename.py), as well as psgcompiler but nothing seems to work. I think I have some problems with the PySimpleGUI module


Solution

  • I am assuming that you are using Windows OS. Which if that is the case then you need to enable freeze_support for all programs that use multiprocessing when trying to "freeze" the program / make an executable.

    For example:

    import time, pyautogui
    import PySimpleGUI as sg
    import multiprocessing
    from multiprocessing import freeze_support
    
    
    def KeepUI():
        
        sg.theme('Dark')
        layout = [
            [sg.Text('Keep-Me-Up is now running.\nClose it to disable it.')]
        ]
        window = sg.Window('Keep-Me-Up', layout)
        
        p2 = multiprocessing.Process(target = dontsleep)
        p2.start()
        
        while True:
            event, values = window.read()
            if event == sg.WIN_CLOSED:
                if p2.is_alive(): 
                    p2.terminate()
                break
    
    def dontsleep():
        while True:
            for i in range(0,100):
                pyautogui.moveTo(1000,i*10)
                time.sleep(120)
    
    
    
    if __name__ == '__main__':
        freeze_support()
        p1 = multiprocessing.Process(target = KeepUI)
        p1.start()