Create a binary file from your python program

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         
No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

You can share the binary with other user.


Reference

Options in pyinstaller

Hi, can it create .bin instead of .exe Many Thanks

Like
Reply
Waqas Younis

Software Engineer | Six-Figure Startup exit at 26

1y

It looks promising but ends up generating a big file. Is there any way to reduce the binary file size? I tried it without using the --onefile flag, which returns a significantly lower binary file, but that binary only works in that directory. If I move it somewhere else, it won't work and provides an error like pyhon3.9.dylib not found. My speculation is --onefile actually bundles the python code in that binary file as well. If that is the case then somehow, there should be an option to use the system's python code base instead of bundling it in the app. Let me know if you have any remedy for this. Thank you

Like
Reply

To view or add a comment, sign in

More articles by Srijan Kafle

Insights from the community

Others also viewed

Explore topics