Transforming your Python GUI App into an Executable Installer

Transforming your Python GUI App into an Executable Installer

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

  • Python 3.x
  • python support IDE (e.g. Pycharm, Visual Studio Code)

Packages prerequisites

  • pyinstaller
  • pysimplegui
  • Image

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:

  1. Open Sandbox on Windows 11.
  2. Copy the Python 'FruitWiki' application.
  3. Paste it into the new Sandbox environment in Windows.

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:

  1. Open Sandbox on Windows 11.
  2. Copy the FruitWiki_Windows_1.0_setup application.
  3. Paste it into the new Sandbox environment in Windows.

Fantastic! It's great to hear that our application is working successfully. ??

Meghna Arora

Quality Assurance Project Manager at IBM

8 个月

Conquer SAFe Certification challenges with www.processexam.com/safe! ?? Your path to success begins here. #Certification #AgileJourney

回复

要查看或添加评论,请登录

Gehan Fernando的更多文章

  • Introduction to gRPC

    Introduction to gRPC

    What is gRPC gRPC is an open-source, high-performance remote procedure call (RPC) framework initially developed by…

  • API Security Best Practices

    API Security Best Practices

    What is API An API, or Application Programming Interface, is a set of rules and protocols that allows different…

  • Understanding and Detecting Memory Leaks in Python

    Understanding and Detecting Memory Leaks in Python

    Introduction Memory leaks can have a significant impact on the performance and reliability of software applications. In…

    2 条评论
  • Middleware for Azure Functions

    Middleware for Azure Functions

    Prior to perusing this article, I recommend reviewing my earlier publication titled "Serverless Programming" However…

    2 条评论
  • Serverless Programming

    Serverless Programming

    What is Serverless programming? Serverless programming is a model of cloud computing where the cloud service provider…

    1 条评论
  • Software Development Methodologies

    Software Development Methodologies

    What is Software development methodology? A software development methodology is a framework or a set of guidelines that…

    1 条评论
  • Let's talk about SOLID principles

    Let's talk about SOLID principles

    History of SOLID SOLID is a set of design principles for object-oriented programming (OOP) that was first introduced by…

  • Introduction to Machine Learning

    Introduction to Machine Learning

    What is Machine Learning Machine learning is a type of artificial intelligence (AI) that enables computer systems to…

社区洞察

其他会员也浏览了