Transforming your Python GUI App into an Executable Installer
Gehan Fernando
Solutions Architect @ 2MNordic IT Consulting AB | .NET | C# | Python | Azure | DevOps | Microservices | Backend Development | Programming | Problem Solving
This tutorial demonstrates how to convert a GUI app created with Python into a standalone executable file and then package it into a setup file for easy distribution.
Software prerequisites
Packages prerequisites
Create the python application
I'm building an application that loads images of fruits and their descriptions into separate folders. I'm using custom fonts for this. I chose PySimpleGUI for the user interface because it's quick and easy, but other frameworks are also an option.
wireframe of the application
folder structure of the application
Great! So far, you have an idea of what the final screen looks like and the folder structure of the project. Download the Code file from | SetupWizard |
In the code you can see the following specific code
def resource_path(relative_path):
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
The resource_path function in the script is designed to provide a reliable way to locate resources (like images, data files, or fonts) used by the application, ensuring that the application works correctly both during development and after being packaged into a standalone executable using PyInstaller.
Open the application via command line
Final screen of the application
Our application is working well, but we have to use the command line to start it each time. It's not functioning like a standalone application. Our next step is to turn the application into an executable file. Then, we can open it without using the command line.
Convert the application into exe
pyinstaller --name FruitWiki --onefile --windowed --icon=Icon.ico --add-data "Fonts;Fonts" --add-data "Images;Images" --add-data "Data;Data" fruitWiki.py
The command I have provided is for using PyInstaller to package a Python application into a standalone executable. Let's break down each argument in the command
--name FruitWiki: This sets the name of the outputted executable file. In this case, the executable will be named FruitWiki.
--onefile: This tells PyInstaller to package the entire Python application into a single executable file. Without this option, PyInstaller generates a folder with an executable and many other files (like .dlls and other dependencies).
--windowed: This option is used for GUI applications. It suppresses the terminal window that normally appears when a console application is run. This is useful for applications with a graphical user interface (GUI) as it makes the app look more like a native app and less like a script being executed.
--icon=Icon.ico: This option sets a custom icon for the executable file. The icon file (Icon.ico) should be present in the directory from which the command is run, or you should provide a full path to the icon file.
--add-data "Fonts;Fonts": This includes additional data files in the packaged application. The syntax is "source;destination". Here, it is copying the Fonts folder from the source (your project directory) to a Fonts folder in the executable's directory. This is necessary for resources like fonts, images, or other data files that your application needs at runtime.
--add-data "Images;Images": Similar to the previous argument, this copies the Images folder into the packaged application.
--add-data "Data;Data": Again, this argument copies the Data folder into the application. This is used for including any additional data files your application might need.
fruitWiki.py: This is the name of your main Python script that you want to package into an executable. This script should contain the entry point of your application.
By using these arguments with PyInstaller, we instructing it to create a single executable file named FruitWiki, with a custom icon, which includes the necessary additional data folders (Fonts, Images, Data), and is suitable for a GUI application.
Before run the pyinstaller command, Application folder looks like
After running the pyinstaller command, Application folder contains with two extra folders.
Open dist folder then we can see our exe file.
领英推荐
To test the application, follow these steps:
Fantastic our application is working well! Since your goal is to distribute the application through a setup file instead of directly as an exe file, the next step is to create a setup project. This setup project will allow you to compile all necessary files into a single installer, which your clients can use to easily install the application on their systems.
Create Setup Project
Before creating the setup project, we need to download | Inno Setup |, Once installed completed click on Script Wizard, then follow the following steps.
You can download open source license The MIT License
Once you click on the Finish button you will get the following popup, then select yes.
Once you click on the yes, you will get the following popup, You can select Yes or No, based on your preference.
Once everything done, goto FruitWiki\Setup folder, you can see our final setup file.
We made a Python application and turned it into an exe file. Then, using Inno, we made an installation setup. The next step is to test the entire application in a new environment.
To test the application, follow these steps:
Fantastic! It's great to hear that our application is working successfully. ??
Quality Assurance Project Manager at IBM
8 个月Conquer SAFe Certification challenges with www.processexam.com/safe! ?? Your path to success begins here. #Certification #AgileJourney