pythonflaskpackagingserver-sent-eventsauto-py-to-exe

Auto py to exe Python Flask app only works with console in windows


I have written a Python based flask app, which sends realtime messages over local network via SSE to conected devices (using flasks integrated server capabilty). As it might be important for this issue, I am using tkinter and customtkinter as GUI.

For anyone reading this, having trouble running auto-py-to-exe with cusotmtkinter in general, please have a look at their documentation realated to possible packaging issues, it helped me a lot.

My Plan:

I want to export my application into a executable app for Windows and Mac using auto-py-to-exe

My Issue:

When I use auto-py-to-exe on Mac to turn my script into an executable app, it works like a charm in windowed mode (console deactivated). I just followed some basic tweaks mentioned in the customtkinter packaging documentation and voila, I have a fully working standalone app.

When I try doing the same on a Windows (10&11) machine, the app will only work if I activate console mode. This means it will always show the console in the background which obviously is not something I would like end users to deal with. Once I build the app in windowed mode, it becomes extremely slow and more or less unresponsive, the moment I start the local server connection from within the app.

My guess:

1: I suspect this has something to do with the Flask-integrated server needing the console to run on Windows to be able to create a stable local connection.

Or

2: Windows can't deal with threading the same way as Mac does and therefore also needs the console to run?

My question:

Has anyone dealt with a similar situation and knows something about issues with Threading or Flask server connections in relation to auto-py-to-exe and Windows?

Please let me know if I need to share more information, I'm not entirely sure how to share my Code as it's stretched across multiple files but will find a way if necessary.

I guess sharing my dependencies might help at this point:

main.py:

import os
import tkinter as tk
import customtkinter as ctk
from tkinter import messagebox, filedialog, ttk, StringVar, Entry, simpledialog
import requests
import json
import platform
import pickle
import threading
from functools import partial
import iso639
import socket
import charset_normalizer
import qrcode
from PIL import ImageTk, Image
from io import BytesIO
import webbrowser

app.py (flask server):

from flask import Flask, render_template, Response, request
from flask_sse import sse
import time
import json
import socket

I have tried using auto-py-to-exe in all sorts of different settings to try and fix this.

My main confusion still is, why it works on Mac without issues but causes trouble in Windows.

Any ideas appreciated.


Solution

  • After searching for further possible causes of my issue, I figured that the problem was being caused by my application actively logging information to the console while running. This caused the application to crash on Windows if no console was initiated.

    Honestly, in the end, I got a solution from ChatGPT:

    Redirect all console outputs to a log file. This can be done using:

    import sys
    sys.stdout = open('my_stdout.log', 'w')
    sys.stderr = open('my_stderr.log', 'w')
    

    So I went ahead and included the ChatGPT solution, which turned out to work.

    So far this is an okay solution, but I am still wondering if there is a smarter way to fix my issue.