Utilized by NASA, ILM, Disney and {hardware} hackers, Python is a flexible programming language and a super selection for learners. Whether or not you’re simply making a “Hey World” or a full-blown software, Python wants an interpreter and a bunch of supporting libraries to work. What if we may make a GUI software, all bundled inside a single executable file?
With auto-py-to-exe ,a undertaking by Brent Vollebregt we will simply create our personal executable Python functions. Beneath the GUI is PyInstaller, a terminal based mostly software to create Python executables for Home windows, Mac and Linux. Veteran Pythonistas will likely be conversant in how PyInstaller works, however with auto-py-to-exe any consumer can simply create a single Python executable for his or her system.
On this , we’re going to create a GUI Python software utilizing EasyGUI, after which use auto-py-to-exe to create a standalone software that may run on any Microsoft Home windows system, together with techniques with out Python put in. Linux and Mac customers might want to use the underlying PyInstaller command line software. A easy app will be created utilizing a single line instruction. By including extra arguments we will embody icons, packaged libraries and so forth.
For instance right here is the code to create a onefile software utilizing app.py because the undertaking code.
pyinstaller --onefile app.py
The place auto-py-to-exe differs is that we’ve a neater means to create an software utilizing a GUI software.
Easy methods to Set up auto-py-to-exe
1. Open a Command Immediate by trying to find CMD.
2. Use the Python package deal supervisor pip to put in auto-py-to-exe.
pip set up auto-py-to-exe
Create a Check Script
Our instance software is an easy GUI to launch one in all three functions. We use the EasyGUI Python library because it abstracts the complexities of making a GUI software. All we have to present is the logic that drives the applying.
1. Open a PowerShell by proper clicking on the Home windows icon and choosing PowerShell.
2. Set up EasyGUI utilizing pip.
pip set up easygui
3. Open a textual content editor to jot down the Python check script. We selected to make use of Notepad++, however you’re free to make use of your favourite editor.
4. Import two Python modules, easygui and os. Easygui creates the GUI software and OS allows the code to work together with the working system.
import easygui
import os
5. Create two variables, one for a message (msg) to the consumer whereas the opposite turns into the applying title.
msg = "Load software..."
title="Tom's {Hardware} Utility Starter"
6. Create a listing, decisions, and inside retailer three values that are the applying names. Lists are Python’s arrays. Objects that may retailer a number of objects. Every merchandise has a numerical index, ranging from zero.
decisions = ["Google Chrome","Slack","PuTTY"]
7. Create an object, reply, to ask the consumer a query. On this case we use a button field from EasyGUI, every button is an possibility from the decisions checklist. The chosen software is saved within the reply object.
reply = easygui.buttonbox(msg, title, decisions=decisions)
8. Use a conditional assertion to learn the worth saved in reply and examine it to a few situations. The primary checks reply to see if it incorporates “Google Chrome” if that’s the case it is going to open the Google Chrome browser. The startfile perform requires the usage of a full file path to the applying. We have to use double within the path as Python makes use of to insert unlawful characters right into a string.
if reply == "Google Chrome":
os.startfile("C:Program FilesGoogleChromeApplicationchrome.exe")
9. Use one other conditional assertion to verify reply for slack.
elif reply == "Slack":
os.startfile("C:UserslespoAppDataLocalslackslack.exe")
10. Add one other conditional assertion to load PuTTY. Notice that for PuTTY we use the os.system perform as PuTTY is a registered app with the Home windows path.
elif reply == "PuTTY":
os.system("putty")
11. Shut the conditional check with an else situation to catch every other enter.
else:
print("Achieved")
12. Save the file as app.py to the Desktop. In case you are utilizing a picture within the software be certain that the picture can be on the Desktop.
Full Instance Code Itemizing
import easygui
import os
msg = "Load software..."
title="Tom's {Hardware} Utility Starter"
decisions = ["Google Chrome","Slack","PuTTY"]
reply = easygui.buttonbox(msg, title , decisions=decisions)
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("Achieved")
Utilizing auto-py-to-exe
1. Open a Command Immediate by trying to find CMD.
2. Run auto-py-to-exe from the immediate.
auto-py-to-exe
3. Click on on Browse and navigate to our instance Python file.
4. Set the applying to make use of one file. This may condense the applying and the supporting Python libraries right into a single executable file.
5. Set the applying to be Console Based mostly. By doing this we are going to see any errors outputted to the Command Immediate. As soon as we’re assured that the app works accurately, we will set this to Window Based mostly.
6. Click on on the Icon drop down and choose an icon in your software. That is an non-compulsory step but it surely provides an additional degree of high quality to your software. Icons should be .ico recordsdata and we used a 64×64 pixel picture as an icon.
7. Click on on Superior and, beneath –title, enter the title of your software. We selected App Launcher.
8. Scroll down and click on on CONVERT .PY to .EXE to begin the method. This may take a few minutes.
9. Click on on Open Output Folder to open the folder containing the applying.
10. Double click on on the icon to run your software.