pythonpyinstaller

PyInstaller - subprocess calling helper script never runs, just opens a new instance of runinng .exe


Relative python newbie here, created (some help by AI) a script to (more) easily manage whitelisting blocked domains from dnscrypt-proxy. An earlier script ran fine calling a restart service helper script, but when i added a right click tray menu option to restart dnscrypt-proxy, i ended up having to use a subprocess to stop the try app (main app) from exiting immediately. when run via .py in console the code works brilliantly

however when compiling i now do not know how to get the helper script to work as even compiling it separately and referencing it at compile time (--add-binary "restart_service_helper.exe;.") doesnt work, clicking the restart option merely launches another instance of the main tray app...

any help would be appreciated

main code - too long for here

https://pastebin.com/kVj1Kj5M

restart_service_helper.py code

import ctypes
import subprocess
import sys

SERVICE_NAME = "dnscrypt-proxy"

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def restart_service():
    subprocess.check_call(["sc", "stop", SERVICE_NAME], shell=True)
    subprocess.check_call(["sc", "start", SERVICE_NAME], shell=True)
    print(f"Service '{SERVICE_NAME}' restarted successfully.")

if __name__ == "__main__":
    if not is_admin():
        # Relaunch the script with admin rights
        ctypes.windll.shell32.ShellExecuteW(
            None, "runas", sys.executable, f'"{__file__}"', None, 1
        )
        sys.exit()

    try:
        restart_service()
    except subprocess.CalledProcessError as e:
        print(f"Failed to restart service: {e}")
        sys.exit(1)

Solution

  • Figured it out myself..

    The easiest way to fix was to point to and external .exe (not a .py) of the helper script and bundle it at compile time to avoid trying to deal with the python interpreter at runtime