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
- Supports creation of a single EXE to install your program for easy online distribution.
- Extensive support for both administrative and non-administrative installations.
- Customizable setup types, e.g. Full, Minimal, Custom.
- Complete uninstall capabilities.
- Running other programs before, during or after install.
- Support for password-protected and encrypted installs.
Prerequisite
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.
- You can put comments in the script by placing semicolon at the beginning of the line.
- It is legal to specify multiple sections of the same name.
Directory Constants
I have included some of the commonly used constants below
- {app}: The application directory, which the user selects on the Select Destination Location page of the wizard.
- {src}: The directory in which the Setup files are located.
- {pf}: The path of the system's Program Files directory.
- {username}: The name of the user who is running Setup or Uninstall program.
- {sd}: System Drive. The drive Windows is installed on, typically "C:".
- {commondesktop}: The path to desktop folder.
- {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
- Open Inno Setup Compiler and select Create a new script file using the Script Wizard option Preview
- Now, press Next button without selecting Create a new empty script file Preview
- In the next widget, fill the application information and click on next button Preview
- You can modify Application Folder information and press next button Preview
- 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
- 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
- In Application File Association dialog box, uncheck Associate a file type to the main executable option and click next button Preview
- In Application Shortcuts dialog box, keep default settings and press next button Preview
- In Application Documentation dialog box, please keep the fields empty and press next button.
- In Setup Install Mode dialog box, select Non administrative install mode radio button and press next Preview
- In Setup languages dialog box, select English check box and press next button Preview
- 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
- In Inno Setup Preprocessor dialog box, keep default setting and press next button. Now, press finish button.
- 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
- 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.
- 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).