How to Make a Windows EXE Executable from Your Python Code

Share Your Love

A tutorial on producing an EXE file from Python code on Windows. That means here you have to produce a Windows EXE executable from your Python code.

If you’re a developer who wants to share his or her work with friends or community members who don’t have a technical or programming background, or with friends who want to perform a specific task (that your code can solve) without having to install or code any IDE, then sharing an EXE file on Windows is your best option.

Let’s look at a complete end-to-end method for creating an EXE file from Python code.

0*eZO6Yd8ayUpPl Eu
One Step conversion of Python to EXE
0*8Vt13oAPppwod89o
Converting Python to EXE

So, let’s get started. To convert Python to EXE, we’ll be utilising auto-py-to-exe, an open-source Python programme.

0*wSvTkCOKOeXFhQFb
Auto Py to EXE UI

The most basic need for installing auto-py-to-exe is that we have Python and pip installed on our developer desktop.

On our command-line shell, run the command pip install auto-py-to-exe to complete the installation.

0*RzFcVLf0JdbkHlLi
pip install auto-py-to-exe

After we’ve installed it, we can run auto-py-to-exe directly from the command line to see the results.

0*qhfmlQ BivElUyPU
Running Auto-py-to-exe

We’ll develop a GUI in Python using minimal code and convert it to EXE as soon as possible. For your convenience, I’ve included the code below.

# creating a Login GUI using Py simple GUI 

#import module Pysimple Gui and named as SG
import PySimpleGUI as sg

# determined the theme for your GUI 
# there are about 8 themes given in pysimple GUI you can get detailes on that by running 
# "sg.theme_previewer()"
sg.theme('DarkAmber')

#determined font for the GUI 
font = ("Times New Roman", 20)

#creating a Layout for our GUI
layout = [
    [sg.Text("A simple login UI Example.",size=(68, 2), justification='centre', font=font)],
    [sg.Text('Enter username : ',size=(20, 1),justification='centre', font=font), sg.InputText(size=(28, 1),enable_events=True, key="-USR-", font=font)],
    [sg.Text('Enter password : ',size=(20, 1),justification='centre', font=font), sg.InputText(size=(28, 1),enable_events=True, key="-PSS-", font=font, password_char='*')],
    [sg.Button('LOGIN',size=(34, 1), font=font,key="-LGN-"),sg.Button('Exit',size=(34,1),font=font)]
]

# Create the window
window = sg.Window("Title is given here", layout,enable_close_attempted_event=True,finalize=True)
#the string is pointed to the title of the UI
#finalize=True determines that the layout is not going to changed at further default it is false 
#setting it true lets you editing the values of buttons, textbox or even the text field
saved_once = False
# Create an event loop
while True:
    # adding events to window.
    event, values = window.read()
    # creating an event for upload button.
    if event == "-LGN-" :
        #getting values from text box.
        usr_nm = str(values["-USR-"])
        pass_ss = str(values["-PSS-"])
        if usr_nm == '':
            # check for username field.
            sg.popup('Notice!!',"username is empty !!")
            continue
        elif pass_ss == '':
            # check for password field.
            sg.popup('Notice!!',"password field is empty!!")
            continue
    # creating an event for exit button
    elif (event == sg.WINDOW_CLOSE_ATTEMPTED_EVENT or event == 'Exit') and sg.popup_yes_no('Do you really want to exit?') == 'Yes':
        #if the user select exit this loop will be ended
        break

window.close()

Simply add your Python script path to the auto-py-to-exe package, as seen in the screenshot below.

0*LJf7dUjbpKvL ZNa

After you’ve finished specifying the script location, click the blue coloured label button, then pick the output destination as seen in the screenshot below.

0*HZppJ4NB4TDvH 70

When you click “CONVERT.PY TO EXE,” the following window will appear:

EXE Build Output
0*POC 4SkImYmnBeOv
Internal Procedure for Building EXE

You’ll have your running GUI EXE file once you open the output file:

0*yJSt4StrnLNU oAp
Click on the GUI_exe

The output will look something like this:

0*3Tn I2fjeufXlugs
Output of EXE

Conclusion

Note: Make sure you have pip installed all of the python packages and libraries required in the python script, as auto-py-to-exe will aggregate all of the libraries accessible on the current machine and generate an EXE file later. If you don’t do this, the EXE file will raise an error saying “no module found” when it’s run on another machine (with the name of the relevant library).

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter