Home System list How to create executable applications in Python

How to create executable applications in Python

0

Used by NASA, ILM, Disney, and hackers, Python is a versatile programming language and an ideal choice for beginners. Whether you’re just creating a “Hello World” or a full application, Python needs an interpreter and a bunch of supporting libraries to work. What if we could create a graphical application, all bundled into a single executable file?

(Image credit: Tom’s Hardware)

With auto-py-to-exe , a project by Brent Vollebregt, we can easily create our own executable Python applications. Below the GUI is PyInstaller, a terminal-based application for creating Python executables for Windows, Mac, and Linux. Veteran Pythonistas will be familiar with how PyInstaller works, but with auto-py-to-exe any user can easily create a single Python executable for their system.

In this tutorial, we will create a Python GUI application using EasyGUI, then use auto-py-to-exe to create a standalone application that will run on any Microsoft Windows system, including systems where Python is not installed. Linux and Mac users will need to use the underlying PyInstaller command line tool. A simple application can be created using a single-line statement. By adding more arguments we can include icons, packaged libraries, etc.

For example here is the code to create a onefile app using app.py as project code.

pyinstaller --onefile app.py

Where auto-py-to-exe differs is that we have an easier way to build an application using a GUI tool.

How to install auto-py-to-exe

1. Open a command prompt by searching for CMD.

(Image credit: Tom’s Hardware)

2. Use the Python package manager pip to install auto-py-to-exe.

pip install auto-py-to-exe

Create a test script

Our sample application is a simple GUI to launch one of three applications. We use the EasyGUI Python library because it abstracts the complexities of building a GUI application. All we need to provide is the logic that drives the application.

1. Open a PowerShell by right-clicking on the Windows icon and selecting PowerShell.

2. Install EasyGUI using pip.

pip install easygui

3. Open a text editor to write the Python test script. We have chosen to use Notepad++but you are free to use your favorite editor.

4. Import two Python modules, easygui and os. Easygui creates the GUI application and the operating system allows the code to interact with the operating system.

import easygui
import os

5. Create two variables, one for a message (msg) to the user while the other becomes the application title.

msg = "Load application..."
title="Tom's Hardware Application Starter"

6. create a list, choices, and inside it store three values ​​which are the names of the applications. Lists are Python arrays. Objects that can store multiple items. Each element has a numeric index, starting at zero.

choices = ["Google Chrome","Slack","PuTTY"]

7. Create an object, Answerto ask the user a question. In this case we are using a buttonbox from EasyGUI, each button is an option of the choices listing. The selected application is stored in the Answer object.

reply = easygui.buttonbox(msg, title,  choices=choices)

8. Use a conditional statement to read the value stored in Answer and compare it to three conditions. The first checks Answer to see if it contains “Google Chrome” if it does, it will open the Google Chrome browser. the boot file requires the use of a full file path to the application. We have to use the double in the path because Python uses to insert illegal characters into a string.

if reply == "Google Chrome":
   os.startfile("C:Program FilesGoogleChromeApplicationchrome.exe")

9. Use another conditional statement to check Answer for the slack.

elif reply == "Slack":
   os.startfile("C:UserslespoAppDataLocalslackslack.exe")

ten. Add another conditional statement to load PuTTY. Note that for PuTTY we use the os.system run as PuTTY is an application registered with Windows path.

elif reply == "PuTTY":
   os.system("putty")

11. Close the conditional test with a other condition to catch any other input.

else:
   print("Done")

12. Save file as app.py in the office. If you’re using an image in the app, make sure the image is also on the desktop.

Complete Example Code List

import easygui
import os
msg = "Load application..."
title="Tom's Hardware Application Starter"
choices = ["Google Chrome","Slack","PuTTY"]
reply = easygui.buttonbox(msg, title , choices=choices)
if reply == "Google Chrome":
    os.startfile("C:Program FilesGoogleChromeApplicationchrome.exe")
elif reply == "Slack":
    os.startfile("C:UserslespoAppDataLocalslackslack.exe")
elif reply == "PuTTY":
    os.system("putty")
else:
    print("Done")

Use auto-py-to-exe

1. Open a command prompt by searching for CMD.

(Image credit: Tom’s Hardware)

2. Run auto-py-to-exe from the prompt.

auto-py-to-exe

(Image credit: Tom’s Hardware)

3. Click Browse and navigate to our sample Python file.

(Image credit: Tom’s Hardware)

4. Configure the application to use a single file. This will condense the application and supporting Python libraries into a single executable file.

(Image credit: Tom’s Hardware)

5. Set the application to be console-based. By doing this we will see all the errors displayed in the command prompt. Once we are satisfied that the application works correctly, we can set it to Window.

(Image credit: Tom’s Hardware)

6. Click the Icon drop-down menu and select an icon for your application. This is an optional step, but it adds an extra level of quality to your application. Icons must be .ico files and we used a 64×64 pixel image as the icon.

(Image credit: Tom’s Hardware)

7. Click Advanced and under -Name, enter the name of your application. We chose the application launcher.

(Image credit: Tom’s Hardware)

8. Scroll down and click CONVERT .PY to .EXE to start the process. It will take a few minutes.

(Image credit: Tom’s Hardware)

9. Click on Open output folder to open the folder containing the application.

(Image credit: Tom’s Hardware)

ten. Double-click the icon to run your application.