Do you want to package your Django project into an easy-to-use EXE file? You're in the right place! This tutorial will guide you through creating an EXE installer for your Django project using Inno Setup, making the deployment process much simpler for your users.


We'll cover everything from setting up Python and Django in a portable environment, running Django as a background service, to adding a desktop shortcut to launch your project in a browser. By the end of this tutorial, you’ll have a fully functioning installer ready for distribution!

What We'll Cover:

  • Setting up a portable Python environment.
  • Installing Django and required libraries.
  • Configuring Django to run as a background service.
  • Creating a desktop shortcut.
  • Setting up a task scheduler.
  • Packaging everything into an EXE installer with Inno Setup.

Folder Structure

Start by creating a folder that will house both Python and your Django project.

Parent-Folder/
   ├── Python/
   ├── ProjectName/

Steps Overview

  1. Create a parent folder for your project.
  2. Install Python inside the parent folder.
  3. Install Django and other libraries (using Parent-Folder/Python/python.exe -m pip install django).
  4. Paste or create your Django project inside the parent folder.
  5. Set up a Windows service to run Django in the background.
  6. Create a scheduled task for running tasks at specific times.
  7. Add a desktop shortcut to open your Django project in the browser.

Prerequisites

Before we dive in, ensure you have the following:

  • Inno Setup: Download it here.
  • Your Django project files ready to go.
  • Python and required libraries installed within your project folder.

Step 1: Setting Up the Basic Inno Setup Script Using Script Wizard

  1. Open Inno Setup Compiler: Launch Inno Setup and select "Create a new script file using the Script Wizard."

    Inno Setup Compiler

  2. Enter Application Information: Provide your app’s name, version, and publisher details.

    Example:

    • App Name: CMS
    • App Version: 1.0
    • Publisher: Easily Learn, Inc.
    • Website: https://easily-learn.blogspot.com/
  3. Select Installation Folder: Uncheck "Allow user to change the application folder" to fix the installation path.

    Folder Path

  4. Add Application Files: When you get to the "Application Files" page, select your parent folder (containing Python and your Django project).

  5. New Setup Wizard: Open Inno Setup and start a new project by selecting Create a new script file using the script wizard.

  6. Application Files Page:

    • Add the Parent Folder where your application files are stored.
    • Click Next.
  7. Application File Association Page:

    • (Optional) If your application opens certain file types, associate those here.
    • Click Next.
  8. Application Shortcut Page:

    • Define shortcuts for the application if needed.
    • Click Next.
  9. Application Documentation Page:

    • (Optional) Add documentation files such as manuals or help files.
    • Click Next.
  10. Setup Install Mode Page:

    • Select the install mode settings for your application (e.g., full or partial installation).
    • Click Next.
  11. Application Registry Keys and Values Page:

    • Define any registry keys if necessary.
    • Click Next.
  12. Setup Languages Page:

    • Choose the preferred installation languages.
    • Click Next.
  13. Compiler Settings Page:

    • Define settings like output folder, compression options, and password protection.
    • Click Next.
  14. Inno Setup Preprocessor Page:

    • Configure any required preprocessor directives (optional).
    • Click Next.
  15. Finish:

    • Click the Finish button to generate the .iss script.
  16. Customization:

    • The script will be generated, and you can add registry entries, tasks, or comments as needed.

Step 2: Create a Desktop Shortcut

Add a shortcut to the user’s desktop that opens your Django project in a browser:

[Icons]
Name: "{userdesktop}\CMS"; Filename: "C:\Program Files\Google\Chrome\Application\chrome.exe"; Parameters: "http://127.0.0.1:8000/"

This will create a desktop icon that opens Google Chrome and navigates to your Django project's local server.


Step 4: Setting Up Django Migrations

We want the installer to automatically run Django migrations after installation. Add the following line to execute python manage.py migrate:

[Run]
Filename: "{app}\python\python.exe"; Parameters: "{app}\django\manage.py migrate"; WorkingDir: "{app}"; Flags: waituntilterminated

This ensures that Django sets up the database after installation.


Step 5: Adding a Scheduled Task

Set up a scheduled task that runs a Django management command daily at midnight:

[Run]
Filename: "{cmd}"; Parameters: "/C schtasks /create /tn CMS_SendReminder /tr \"\"{app}\python\python.exe {app}\django\manage.py send_reminders\"\" /sc daily /st 00:00:00 /ru SYSTEM /rl HIGHEST /f"; Flags: runhidden

Step 6: Creating a Background Service to Run Django

Create a batch file (start.bat) to run the Django server in the background:

@echo off
cd /d "%~dp0"
"%~dp0python\python.exe" "%~dp0django\manage.py" runserver 0.0.0.0:8000

Add these lines to your script to create and start a Windows service that runs this batch file:

[Run]
Filename: "{cmd}"; Parameters: "/C sc create CMS_Service binPath= \"\"{app}\start.bat\"\" start= auto"; Flags: runhidden
Filename: "{cmd}"; Parameters: "/C sc start CMS_Service"; Flags: runhidden

Step 7: Compile and Test Your Installer

Once everything is set up, your script should look like this:

#define MyAppName "CMS"
#define MyAppVersion "1.0"
#define MyAppPublisher "Easily Learn, Inc."
#define MyAppURL "https://easily-learn.blogspot.com/"

[Setup]
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
OutputBaseFilename=setup

Click Compile to generate your installer and run it to test your new EXE!


Sample .ISS Questions 

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "VRS"
#define MyAppVersion "1.0"
#define MyAppPublisher "6f Tech Consultant India Private Limited"
#define MyAppURL "https://6ftechconsultant.com/"

[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{EEBCF04D-5F13-4C07-9383-D5A64F279ACF}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={commonpf}\KenshinSystems\{#MyAppName}
DisableDirPage=yes
DefaultGroupName={#MyAppName}
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputBaseFilename=vrs
Compression=lzma
SolidCompression=yes
WizardStyle=modern
PrivilegesRequired=admin

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Files]
Source: "C:\Users\Admin\Downloads\6F Tech\NTN\VRS\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
; Create a shortcut on the desktop that opens Chrome with a specific URL
Name: "{userdesktop}\VRS Web"; Filename: "C:\Program Files\Google\Chrome\Application\chrome.exe"; Parameters: "http://127.0.0.1:8000/"

[Run]
; Run the Django migration command after the installation
; Ensure Python and Django are installed, and run the command from the installation directory
Filename: "{app}\python\python.exe"; Parameters: "{app}\django\manage.py migrate"; WorkingDir: "{app}"; Flags: waituntilterminated

; Add a scheduled task to run a Python script to send mail reminder daily at 00:00:00
Filename: "{cmd}"; Parameters: "/C schtasks /create /tn VRS /tr ""{app}\python\python.exe {app}\django\manage.py send_expiry_notifications"" /sc daily /st 00:00:00 /ru ""SYSTEM"" /rl HIGHEST /f"; Flags: runhidden

; Create a Windows service to run Django in the background
Filename: "{cmd}"; Parameters: "/C sc create VRS_Service binPath= ""{app}\start.bat"" start= auto"; Flags: runhidden

; Start the service after installation
Filename: "{cmd}"; Parameters: "/C sc start VRS_Django_Service"; Flags: runhidden

Conclusion

With just a few steps, you can create an EXE installer for your Django project, making it easy for users to install and run your web application. By using Inno Setup, you’ve automated the setup of your project and ensured that it runs smoothly on your users’ machines.

Happy coding!