Preface
When you share it with users without a Python environment after developing a Python application, it is not convenient to directly distribute the source code. This article will teach you how to package Python scripts into Windows executables (.exe) and further make them into an installer with a setup wizard.
Step 1: Package Python scripts using auto-py-to-exe
auto-py-to-exe
is a user-friendly tool for packaging Python scripts into standalone executables. The following are the detailed operations:
1.1 Install auto-py-to-exe
Run the following command to install in the command lineauto-py-to-exe
:
pip install auto-py-to-exe
1.2 Start the graphical interface
After the installation is completed, run the following command to startauto-py-to-exe
Graphic interface:
auto-py-to-exe
1.3 Configuring Packaging Options
In the pop-up graphical interface:
-
Script Location: Select your Python script file (e.g.
your_script.py
)。 - Onefile: Check "Onefile" to package all content into a single file.
-
Console Window:
- If it is a GUI program, select Window Based (hide the console).
- If it is a command line program, select "Console Based".
-
Icon: Optional, if you need a custom icon, you can choose one
.ico
document.
ClickConvert .py to .exe, waiting for the packaging to be completed. After the package is successful, the generated.exe
The file is usually located inoutput
In the folder.
1.4 Test generated .exe file
Found the generated.exe
Double-click to run the file to ensure that the program works normally. If everything is OK, you can move on to the next step.
Step 2: Create the installer using Inno Setup
In order to.exe
Pack the file into an installable program (with installation wizard), we use special toolsInno Setup。
2.1 Download and install Inno Setup
- Go toInno Setup Official Download PageDownload the latest version.
- Follow the prompts to complete the installation.
2.2 Create an installation script
Open Inno Setup and select“Create a new script file using the Script Wizard”。
-
Follow the wizard to fill in the information:
-
Application Information:
- Application name: Fill in the application name (such as "My App").
- Application version: Fill in the version number (such as "1.0").
- Application publisher: Fill in the publisher name (such as “My Company”).
-
Application Files:
- Add generated
.exe
Files, e.g.C:\Users\YourName\output\your_program.exe
。
- Add generated
-
Application Shortcuts:
- Check "Create a shortcut for this application" and set the shortcut name.
-
Application Documentation:
- Optional: If there is a license agreement file or help document, you can add it in this step.
-
Setup Languages:
- The default is the English installation interface, and you can also choose another language.
-
Compiler Output:
- Set the location where the installer is generated, for example
C:\Users\YourName\output\
。
- Set the location where the installer is generated, for example
-
After completion, Inno Setup will generate a
.iss
File (script file).
2.3 Compile and install the program
- Click on the top of the Inno Setup interface“Compile”button.
- After compilation is completed, find the generated installer in the output directory (such as
)。
Step 3: Test the installer
- Run generated
, follow the prompts to complete the installation.
- Check the following:
- Whether it is correctly installed in the specified directory (such as
C:\Program Files\YourApp
)。 - Whether to generate a desktop shortcut.
- Whether the program can run normally.
- Whether it is correctly installed in the specified directory (such as
Additional Tips
Reduce packaged file size
If the packaged file is too large, you can try the following method:
- exist
auto-py-to-exe
Enable the "Advanced" option in to manually exclude unwanted dependencies. - Make sure that only the actual required dependencies are installed.
Custom installation interface
Inno Setup supports highly customization and can be edited.iss
The script adds background pictures, custom text, etc. of the installation interface.
Example: Add a background image to the installer:
[Setup] WizardImageFile=path\to\your\ WizardSmallImageFile=path\to\your\small_image.bmp
More friendly user experience
- Add program icon: in
auto-py-to-exe
and Inno Setup respectively specify icon files. - Added uninstall function: Inno Setup generates an uninstaller by default, and users can remove the software through the control panel.
Summarize
Through the guidance of this article, you can easily package Python scripts into separate executables and create a complete installer for easy installation and use. Here are the core steps:
- use
auto-py-to-exe
Package Python scripts as.exe
document. - Use Inno Setup to create the installer.
- Test whether the packaged program is running normally.
This is the article about using Python to package programs and making Windows installers. For more related content on Python packaging and making Windows installers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!