Create a binary file from your python program
In my previous article, I explained how to convert your bash script to a binary executable. In this article we will be exploring creation of standalone binary executable from Python program
If you want to distribute a Python program to others, you may not want to give them access to your source code. One way to do this is by converting your Python script to an standalone executable binary file. In this article, we’ll show you how to use PyInstaller to convert a Python script to an executable binary file.
Step 1: Create the Python Script
First, create the Python script that you want to convert to an executable binary file. Save this file with a .py extension.
For this example, let’s create a simple Python script that prints “Hello, World!” to the console. Save this script as hello.py:
#!/usr/bin/python3
print("#######################################################")
print(" __ __ __ __ ")
print(" / /_/ /_ ___ ____ ____ ____ / /_ ___ _____/ /_")
print(" / __/ __ \/ _ \/ __ \/ __ \/ __ \/ __ \/ _ \/ ___/ __/")
print("/ /_/ / / / __/ / / / /_/ / /_/ / /_/ / __/ / / /_ ")
print("\__/_/ /_/\___/_/ /_/\____/\____/_.___/\___/_/ \__/ ")
print("#######################################################\n\n")
print("Hello World!!!")
print("This script will be converted to binary file")
Step 2: Install PyInstaller
Next, you’ll need to install PyInstaller. You can install it using pip:
pip install pyinstaller
Step 3: Convert the Script
Once PyInstaller is installed, navigate to the directory where your Python script is located and run the following command in your terminal:
pyinstaller --onefile hello.py
The --onefile switch tells PyInstaller to package the script and all its dependencies into a single executable file.
There are other switches available to use according to you preferance. Refer to references for more.
Step 4: Locate the Binary File
After running the command, PyInstaller will create two directories in the same directory where your Python script is located: dist and build. The binary file will be located in the dist directory.
You can share the binary with other user.