Part 4 - Creating a launcher

 

import sys
import time
import psutil
import subprocess
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


if getattr(sys, 'frozen', False):
    root_dir = Path(sys.executable).parent
else:
    root_dir = Path(__file__).resolve().parent


python_embedded = root_dir / "python-3.12.8-embed-amd64" / "python.exe"
django_project_dir = root_dir / "mysite"
manage_py = django_project_dir / "manage.py"

django_command = [str(python_embedded), str(manage_py), "runserver"]

process = subprocess.Popen(django_command, cwd=django_project_dir, creationflags = 0x08000000)


chrome_options = Options()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_argument("--window-size=1240,740")
chrome_options.add_argument("--app=http://localhost:8000")
driver = webdriver.Chrome(options=chrome_options)

try:
    while True:
        time.sleep(2)
        driver.title
        
except Exception as e:
    driver.quit()


for child in psutil.Process(process.pid).children(recursive=True):
    child.terminate()




Converting to a .exe file
 

pip install pyinstaller==5.13.2
pyinstaller --onefile --noconsole --icon=logo.ico launcher.py

 

  • The logo.ico is optional, if you skip it then the default icon will be used.
  • Two folders and a file will be generated. The .exe file is inside the dist folder.
     

Windows


10 Jan. 2025 | Last Updated: 25 Nov. 2025 | jaimedcsilva

Related
  • Part 1 - Example of a Django project
  • Part 2 - Embedded Python + pip
  • Part 3 - Modifying manage.py
  • Part 4 - Creating a launcher
  • Part 5 - Windows Installer with Inno Setup

  • Buy Me a Coffee