Python service wrapper (windows)

Python is installed on the server and the Flask app runs fine (manually from PowerShell). Install pywin32 for Windows service support (globally, not in myenv). The application uses environment (.env) variables not seen by the SYSTEM user.
 
pip install pywin32

python 'D:\Program Files\Python39\Scripts\pywin32_postinstall.py' -install
python -c "import servicemanager; print('OK')"

pip list
    Package            Version
    ****** ***--
    pywin32            311
    ...

Create a Service Wrapper

Ccreate the service wrapper (flask_service.py) and install/start the service: Create a new Python file, e.g., flask_service.py:
 
#cd D:\python-apps\my_app

import win32serviceutil
import win32service
import win32event
import servicemanager
import subprocess
import sys
import os

class FlaskService(win32serviceutil.ServiceFramework):
    _svc_name_ = "FlaskAppService"
    _svc_display_name_ = "Flask Application Service"
    _svc_description_ = "Runs Flask app as a Windows Service"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        self.process = None

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        if self.process:
            self.process.terminate()

    def SvcDoRun(self):
        servicemanager.LogInfoMsg("Starting Flask Service...")
        # Path to your virtual environment Python
        venv_python = r"D:\python-apps\my_app\myenv\Scripts\python.exe"
        # Path to your Flask app
        app_script = r"D:\python-apps\my_app\app.py"
        # Start Flask app
        self.process = subprocess.Popen([venv_python, app_script])
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(FlaskService)

Install and Start the Service

Run these in PowerShell as Administrator:
 
PS D:\python-apps\my_app>python flask_service.py install
PS D:\python-apps\my_app>python flask_service.py start

Steps to Reinstall the Service

Stop the service (if it's running):
 
python flask_service.py stop
python flask_service.py remove

python flask_service.py install
python flask_service.py start

Extra

Install requirements on production.
 
(myenv) pip freeze > requirements.txt
(myenv) pip install -r requirements.txt
Pip settings (C:\ProgramData\pip\pip.ini)
 
[global]
trusted-host=artifactory.mydomain.com
index-url=https://artifactory.mydomain.com/artifactory/api/pypi/py-pypi-remote/simple

Reinstall services (python global)

1. pywin32 pip install pywin32 python "D:\Program Files\Python312\Scripts\pywin32_postinstall.py" -install python -c "import servicemanager; print('OK')" python --version 3.12.7 access_log cd D:\python-apps\access_log python flask_service.py stop python flask_service.py remove python flask_service.py install python flask_service.py start