Create Installer of Python Program for Windows OS using Inno Setup

Create Installer of Python Program for Windows OS using Inno Setup

Create installer from executable built using PyInstaller in Windows OS

Introduction

This article is continuation of article Bundle Large Python Application using PyInstaller. Before moving to next section, it is recommended to read the previous article.

Inno Setup is a free installer for Windows programs. Some of it's key features are

  1. Supports creation of a single EXE to install your program for easy online distribution.
  2. Extensive support for both administrative and non-administrative installations.
  3. Customizable setup types, e.g. Full, Minimal, Custom.
  4. Complete uninstall capabilities.
  5. Running other programs before, during or after install.
  6. Support for password-protected and encrypted installs.

Prerequisite

  1. Download and Install Inno Setup (Preview)

Script Format

Inno Setup scripts are arranged into sections. Each section controls a different aspect of the installation. A section is started by specifying the name of the section enclosed in square brackets []. Inside each section is any number of entries.

  1. You can put comments in the script by placing semicolon at the beginning of the line.
  2. It is legal to specify multiple sections of the same name.

Directory Constants

I have included some of the commonly used constants below

  1. {app}: The application directory, which the user selects on the Select Destination Location page of the wizard.
  2. {src}: The directory in which the Setup files are located.
  3. {pf}: The path of the system's Program Files directory.
  4. {username}: The name of the user who is running Setup or Uninstall program.
  5. {sd}: System Drive. The drive Windows is installed on, typically "C:".
  6. {commondesktop}: The path to desktop folder.
  7. {group}: The path to the Start Menu folder, as selected by the user on Setup's Select Start Menu Folder wizard page. This folder is created under the All Users profile unless the user installing the application does not have administrative privileges, in which case it is created in the user's profile.

Setup section

This section contains global settings used by the installer and uninstaller. Certain directives are required for any installation you create. Here is an example of a Setup section:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
DefaultGroupName=My Program
  • AppName: This required directive specifies the name of the application being installed.
  • AppVersion: This directive specifies the version number of the application being installed.
  • DefaultDirName: The value of this required directive is used for the default directory name, which is used in the Select Destination Location page of the wizard.
  • DefaultGroupName: The value of this directive is used for the default Start Menu folder name on the Select Start Menu Folder page of the wizard.
  • You can read official documentation of setup section to know more.

Files section

This optional section defines any files Setup is to install on the user's system. Here is an example of a Files section:

[Files]
Source: "CTL3DV2.DLL"; DestDir: "{sys}"; Flags: onlyifdoesntexist uninsneveruninstall
Source: "MYPROG.EXE"; DestDir: "{app}"
Source: "MYPROG.CHM"; DestDir: "{app}"
Source: "README.TXT"; DestDir: "{app}"; Flags: isreadme
  • Source: The name of the source file. This can be a wildcard to specify a group of files in a single entry. When a wildcard is used, all files matching it use the same options.
  • DestDir: The directory where the file is to be installed on the user's system. Will almost always begin with one of the directory constants.
  • Please follow official documentation of Files section to know more.

Run section

Run section is optional, and specifies any number of programs to execute after the program has been successfully installed, but before the Setup program displays the final dialog. Programs are executed in the order they appear in the script. By default, when processing a Run entry, Setup will wait until the program has terminated before proceeding to the next.

[Run]
Filename: "{app}\INIT.EXE"; Parameters: "/x"
Filename: "{app}\README.TXT"; Description: "View the README file"; Flags: postinstall shellexec skipifsilent
Filename: "{app}\MYPROG.EXE"; Description: "Launch application"; Flags: postinstall nowait skipifsilent unchecked

You can read official documentation to know about more.

Other important sections

Inno Setup supports collection of different sections. As I have covered, few commonly used sections but depending upon the requirements, you can use other sections. I recommend to have a look on Dirs, Icons, Tasks, Registry and Code sections.

Create Installer

We will utilize the same python project that used for creating executable using PyInstaller. Please clone this github repository and follow instructions in README.md file to create executable with supporting files. Please follow below steps to build installer

  1. Open Inno Setup Compiler and select Create a new script file using the Script Wizard option Preview
  2. Now, press Next button without selecting Create a new empty script file Preview
  3. In the next widget, fill the application information and click on next button Preview
  4. You can modify Application Folder information and press next button Preview
  5. This is very important step. Please enter Application main executable file field to path of main.exe in your project (PROJECT_FOLDER\executable\dist\main\main.exe) Preview
  6. Now, click on Add folder... button and select your PROJECT_FOLDER\executable\dist\main folder. Please select Yes option to include files and subfolder of selected folder. You can click next button Preview
  7. In Application File Association dialog box, uncheck Associate a file type to the main executable option and click next button Preview
  8. In Application Shortcuts dialog box, keep default settings and press next button Preview
  9. In Application Documentation dialog box, please keep the fields empty and press next button.
  10. In Setup Install Mode dialog box, select Non administrative install mode radio button and press next Preview
  11. In Setup languages dialog box, select English check box and press next button Preview
  12. In Compiler Settings dialog box, fill the Custom compiler output folder field as PROJECT_FOLDER\Installer and Custom output base file name as Image_Viewer Preview
  13. In Inno Setup Preprocessor dialog box, keep default setting and press next button. Now, press finish button.
  14. You can run the generated script now by saving it in PROJECT_FOLDER\installer folder or you can run later by clicking run button (F9) Preview
  15. You can check status of build operation on log window at bottom. Inno setup will ask to install the application which you can do by selecting default settings.
  16. After installation of application, you can search Image Viewer in your windows search bar. I have included installer.iss file inside installer folder of github project for your reference.

ProTip: Check Examples folder in Inno Setup installed folder(Example: C:\Program Files (x86)\Inno Setup 6\Examples).

Reference

  1. Inno Setup documentation PDF