Convert A Python Script To Executable File (with GUI)

Sharing is caring!

Last Updated on July 14, 2022 by Jay

Recently I found a neat tool for converting a Python script to an executable file using a graphical user interface. You might be already familiar with freezing Python scripts with pyinstaller, that’s cool if you prefer to use the command line. For those who prefer having a graphical interface, then auto-py-to-exe is a good choice, and it’s super easy to use.

By converting a Python script to an executable file, we can send it to our friends to run on their computers even if they don’t have Python installed.

Before We Start

  1. Make sure to remove your id and password before sending anything out to other people!!
  2. It’s better to use a virtual environment for our project, so that Pyinstaller has the absolute minimal libraries to work with. Make sure all required libraries are installed in the virtual environment. You can learn how to setup a virtual environment using this tutorial.

pip install auto-py-to-exe

Once installed, type “auto-py-to-exe” in the console to open this program (shown below).

This module is essentially a GUI wrapper for the pyinstaller library, as we can see in the Current Command section of the program interface.

Graphical User Interface of auto-py-to-exe
Graphical User Interface of auto-py-to-exe

Step 1. Select A Python Script

I’m going to use the Amazon price tracking bot script for this conversion, so I’ll go ahead and select that. You can feel free to use any Python script.

Step 2. Choose One File or One Directory Format

We can choose whether to convert the script into a directory, or a single file.

I prefer to convert my script into just one single file so I don’t have to deal with many files inside a directory. So I’m going to select the “One File” option.

Step 3. Choose An Output Folder

After conversion, we need to find the Application file in the output directory. By default, the Application name is the same as the script name. You can find or change the output directory in the Settings section of the program.

Settings in auto-py-to-exe
Settings in auto-py-to-exe

Step 4. Adding Additional Files To Application

When we give this application to another person, ideally they just double click it and the program runs. So it’s important to include any required files together with our application. Since our applicaiton requires the chromedriver, we’ll add it to the package as well. In the “Additional Files” setting, we can choose to add a single file or a folder.

When we use the “One File” together with the “Additional Files” options. We need to tweak our code a little bit, otherwise the final application won’t work. Pyinstaller unpacks the data/files into a temporary folder called sys._MEIPASS. Therefore, we have to instruct our code to pickup the files from the right place. We are going to create a function to include the appropriate folder for our added files.

def file_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception as e:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)


## include the appropriate folder address for chromedriver.exe
driver_path = file_path(r'chromedriver.exe')
driver = webdriver.Chrome(driver_path, options=opt)

Step 5. Adding An Icon To Application (Optional)

I usually prefer to have a custom icon for my application, so let’s add that.

To add an icon for our application. Go to the Icon section, I’m going to select the icon file for this application. Note the icon has to be a .ico file format. I use this free site to convert pictures (.jpg or .png) to ico files.

With the above steps, we are ready to convert our Python script into an executable file. Next just click on the “CONVERT .PY TO .EXE” and wait for it to do the job.

WARNING On Using A Virtual Environment

If you are using auto-py-to-exe inside a virtual environment, make sure to install all libraries required by your script. Otherwise, the final executable file will be missing libraries, and will not run.

Check The Executable File

Now we have this executable file, let’s run it to test. Double click on the file, and it should open up a browser on the Amazon product page, check the price then send myself an email & a WhatsApp msg.

Leave a Reply

Your email address will not be published. Required fields are marked *