Create Executable of Python Script using PyInstaller

In this short guide, you’ll see the how to create an executable of a Python script using PyInstaller.

Here are the steps:

Steps to Create an Executable using PyInstaller

Step 1: Add Python to Windows Path

To start, you may want to add Python to Windows path.

An easy way to add Python to the path is by downloading a recent version of Python, and then checking the box to ‘Add Python to PATH’ at the beginning of the installation:

Add Python to PATH

Finish the installation, and you should be good to go.

Step 2: Install the PyInstaller Package

Next, open the Windows “Command Prompt” and then type the following command to install the PyInstaller package:

pip install pyinstaller

Step 3: Save your Python Script

Save your Python script at your desired location.

For example, let’s create a simple Python script that displays ‘Hello World!’ when clicking a button:

import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()


def hello():
    label = tk.Label(
        root, text="Hello World!", fg="blue", font=("Arial", 18, "bold")
    )
    canvas.create_window(150, 200, window=label)


button = tk.Button(text="Click Me", command=hello, bg="brown", fg="white")
canvas.create_window(150, 150, window=button)

root.mainloop()

For demonstration purposes, let’s say that the Python script is stored in the following folder:

C:\Users\Ron\Desktop\Test

Where the Python script is called ‘hello‘ and the file extension is ‘.py

Step 4: Create the Executable using PyInstaller

Now you’ll be able to create the executable of the Python script using PyInstaller.

Simply go to the Command Prompt, and then type:

cd, followed by space, and then the location where your Python script is stored

Here is the command for our example:

C:\Users\Ron>cd C:\Users\Ron\Desktop\Test

Press Enter (after you typed the location where the Python script is stored on your computer).

Then, refer to the following template to create the executable:

pyinstaller --onefile python_script_name.py

Since for our example, the python_script_name is ‘hello‘ (and the file extension is .py), then the command to create the executable is:

pyinstaller --onefile hello.py

Press Enter for the last time.

Note that if you wish to suppress the terminal window that opens with the program, then use the following command instead:

pyinstaller --noconsole python_script_name.py

For our example:

pyinstaller --noconsole hello.py

Step 5: Run the Executable

Your executable will be created at the location that you specified.

For our example, it will be under the same folder where the ‘hello’ script was originally stored:

C:\Users\Ron\Desktop\Test

You’ll notice that few additional files were created at that location.

To find the executable file, open the dist folder. You’ll then see the executable file:

hello

Double click on the file, and you should be able to launch your program.

In our case, once you click on the ‘hello’ executable, you’ll get a display with a single button.

And if you click on that button, you’ll see the following expression:

Hello World!

You can read more about PyInstaller by visiting the PyInstaller manual.