Header image

How to create windows exe file from python application made with Poetry, Tkinter using PyInstaler or auto-py-to-exe

10

November 03, 2024

coding

Introduction

You have developed an application using Poetry as dependency management for your python application. Once it’s done, you have deceived to create a UI for that python code and make it more user-friendly using Python Tkinter library or module.

Everything works fine, and you are satisfied with the product now just decided to ship this Python code to .EXE file to make it executable on the Windows Platform.

There is a popular Python core written PyInstaller module which sole purpose is to convert .py code to .exe executable.

For a straight forward python code with no major dependency management like venv, poetry. It’s easy to set up and work like magic. But in our case we have poetry which will abstract the virtual environment system somewhere else. Which as developer we need to make some extra step and guide PyInstaller to go to that path and fetch all necessary code / modules.

Install auto-py-to-exe

You can simply open a terminal and run pip install auto-py-to-exe

pip install auto-py-to-exe
Why auto-py-to-exe?

Instead of PyInstaller, I have decided to use this open source application module which basically provides a GUI layer over CLI based PyInstaller it is a lot convenient and easy to use.

In order to run this auto-py-to-exe we need to follow poetry shell cmd in the project directory

Open a terminal in your project directory and run poetry shell

poetry shell
Then run auto-py-to-exe

You would see a GUI for auto-py-to-exe

Img could not load

We will go step by step and fill all the necessary field

In the script location

Give the main.py file or the file which has the python code regarding initial code. Most of the cases its going it app.py, main.py, start.py

Section 2 – Onefile or One Directory

I would advise One Directory, because we are using modules which are residing in different location and people often recommend One Directory when we deal with Tkinter.

Section 3 – Console Window.

This step depends on your application, if some data is being displayed on the console. And you want that to be visible to the end user, it’s wise to chose Console Based. Or else Windows Based where there will not be any console.

Icon section

When the application is build, there will be an icon for that .exe file. To change the icon to a desired one

Additional Files **

This is the important section in this process. Here we will provide all the files we have created in the project which are a part of the project, such as .py files, images, videos, Excel sheet. What ever files which are crucial for the application Example: My project needs the .whl file and the Excel file and all the .py files which are imported but still we needed to add that in the additional files section.

Img could not load

Advanced section

This is where all our imports are bundled. There are 2 ways to do this, either add all imports one by one in the hidden section which is what most secure application do. But my application and most cases we just want things to work and we dont want to share the code to the end users and make them run terminal commands which might be issue to some. In these uses I’m going with –collect-all which will add all those code which is needed for the application.

What do we need to add in this section

In the simple words, we need to add all the dependencies which are present in the .toml file generated by the poetry add cmds. Add each of the dependencies except python as its not needed.

Img could not load

Once all these are filled, scroll to the end, we shall see the option convert .Py to .Exe Clicking it begins the process of building the executable file as we have selected -collect-all, this will take more time than general.

Img could not load

Once the process has been done the output folder will be created which will have a simple .py file. In my case, ui.py was the main file which beings the process.

Img could not load

By clicking that, it will open the Tkinter UI. which is made for the application by the developer. Therefore, this is by far the easy way to make this process working. I really appreciate if any reader is aware of a better and secure way please share it with me.