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.

The following video tutorial is also available:

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.

4 thoughts on “Create Executable of Python Script using PyInstaller”

    • It should also work with pygame. For example, I created a simple script with pygame, which I called main.py. I also added a background image under the same directory, called my_image.png.
      I then applied this command in the terminal:

      pyinstaller --onefile --add-data "my_image.png;." main.py

      The executable in the dist directory launched the game successfully.
      Note that in order to load the background image, I had to add the following lines to the main.py script:

      import sys
      import os
      
      base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
      background_path = os.path.join(base_path, 'my_image.png')
      background = pygame.image.load(background_path)
      

      The above involves using ‘_MEIPASS’, which PyInstaller sets to point to the temporary folder where it extracts files during execution.
      I hope it helps 🙂

      Reply
        • Hey, try the following approach:

          1. Create a project directory (let’s call it new_project), organized in a structured manner.

          For example:

          new_project/
          ├── main.py
          ├── data/
          │   ├── data_file.txt
          ├── assets/
          │   ├── image.png
          ├── other_module.py
          

          2. In the command prompt point to the path of the new_project

          3. Then use this template:

          pyinstaller --onefile --add-data "C:\\Path\\To\\new_project\\data;data" --add-data "C:\\Path\\To\\new_project\\assets;assets" C:\\Path\\To\\new_project\\main.py
          

          For example:

          pyinstaller --onefile --add-data "C:\\Users\\my_user\\new_project\\data;data" --add-data "C:\\Users\\my_user\\new_project\\assets;assets" C:\\Users\\my_user\\new_project\\main.py
          
          Reply

Leave a Comment