Create an Executable of a Python Script using PyInstaller
In this very short tutorial, you will convert a Python script into an executable file using PyInstaller.
TLDR solution
pyinstaller --noconsole script.py
Step-by-Step Example
Step 1: Install the PyInstaller Package
Execute the following command in your terminal/command prompt:
pip install pyinstaller
Step 2: Create and Save Your Python Script
For instance, let's create a a simple script that creates a text file on your desktop:
script.py
import os
with open(os.path.expanduser("~/Desktop") + "/helloworld.txt", "w") as file:
file.write("Hello World!")
Step 3: Create the Executable using PyInstaller
Let's say that this script is saved on your desktop in a folder called Test. Navigate your terminal to this folder:
cd Desktop/Test
From inside this folder, simply run the following command:
pyinstaller --onefile script.py
Note that the --onefile option creates an executable file which opens a terminal window when opened. To suppress this window, use --noconsole instead:
pyinstaller --noconsole script.py
This creates two folders next to your script:
build: contains intermediate outputs (you can safely ignore it)dist: contains your executable file
Verify your excutable file by double clicking it.
That's it! You just created an executable of your Python script.