1. Introduction

Unquestionably, owing to its ease of use and readability, Python has been a versatile and popular programming language. Nevertheless, we might sometimes want to transform our Python source code into a binary format.

So, in this tutorial, we’ll learn the importance of a binary executable and explore different tools, namely PyInstaller, py2exe, and Nuitka, to convert a .py file into a binary format.

2. Why Convert Python to Binary?

Before learning to convert a Python (.py) file to a binary format, let’s first understand the underlying reasons:

  • Distribution: distributing a Python application to users who may not have Python installed on their systems becomes more accessible when we have a standalone binary, eliminating the need to install Python and its prerequisites separately
  • Intellectual Property Protection: makes it harder for others to decipher, reverse engineer, or tamper with the code
  • Improved Performance: reduces startup time, boosts execution speed, and eliminates the need for the Python interpreter

3. PyInstaller

PyInstaller converts Python scripts into independent executable files that run on various operating systems, such as Windows, macOS, and Linux. Now, let’s understand how to create a binary file using PyInstaller.

Firstly, we download and install PyInstaller and its associated files using pip:

$ sudo pip install pyinstaller
Collecting pyinstaller
  Downloading pyinstaller-6.0.0-py3-none-manylinux2014_x86_64.whl (657 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 657.0/657.0 KB 1.1 MB/s eta 0:00:00
...
... output truncated ...
...

Next, let’s open the terminal and navigate to the directory where the Python script (.py file) is located. It’s a straightforward test script written using Python and executed using Python3:

$ cat baeldung_python_binary_demo.py
#!/usr/bin/python
print ("Welcome to Baeldung Article")

$ python3 baeldung_python_binary_demo.py
Welcome to Baeldung Article

Now, let’s use the PyInstaller to convert this Python script into a binary file:

$ pyinstaller --onefile baeldung_python_binary_demo.py
118 INFO: PyInstaller: 6.0.0
118 INFO: Python: 3.10.12
119 INFO: Platform: Linux-6.2.0-33-generic-x86_64-with-glibc2.35
120 INFO: wrote /home/labenv/baeldung_python_binary_demo.spec
...
... output truncated ...
...
8786 INFO: Appending PKG archive to custom ELF section in EXE
8827 INFO: Building EXE from EXE-00.toc completed successfully.

Here, we can replace baeldung_python_binary_demo.py with the actual name of our Python script:

$ tree
.
├── build
│   └── baeldung_python_binary_demo
│       ├── Analysis-00.toc
...
... output truncated ...
...
├── dist
│   └── baeldung_python_binary_demo
...
... output truncated ...
...

Eventually, we generate a binary executable in the dist directory within our project folder. The executable file has the same name as our original Python script, albeit without the .py extension:

$ ./dist/baeldung_python_binary_demo
Welcome to Baeldung Article

Now, we can easily execute the created binary as any other executable using “./”. Also, we can ship this executable to any system with the same OS configuration.

Evidently, PyInstaller seamlessly creates standalone executables from Python scripts, thereby facilitating portability and ease of distribution across various operating systems.

Further, this tool combines the script and its dependencies into a single executable, eliminating the need for a separate Python interpreter on the target machine.

4. Py2exe (Windows Only)

Py2exe is a Windows-specific tool that simplifies converting Python scripts into Windows executables, allowing users to run Python programs without a Python interpreter.

First, let’s install py2exe using pip:

$ pip install py2exe

Subsequently, we download and install py2exe and its dependencies. Then, we create a Python script exec_convertor.py that configures py2exe.

In this script, we import the necessary modules and specify the main script baeldung_python_binary_demo.py we want to convert:

$ cat exec_convertor.py
from distutils.core import setup
import py2exe
setup(console=['baeldung_python_binary_demo.py'])

Here, we use the setuptools library for packaging, including its metadata and installation instructions. Also, the console argument specifies the command-line entry points when the package is installed. Now, we replace baeldung_python_binary_demo.py with the name of the Python script we want to convert and execute the setup script:

$ python setup.py py2exe

This command triggers py2exe to package the Python script into a Windows executable.

Now that the setup script has run, we’ll find the binary executable in the dist directory within the project folder. Again, the executable file has the same name as our original script but without the .py extension.

In such a way, Py2exe streamlines the process of creating Windows executables from Python scripts and makes it easier to distribute Python applications on Windows systems.

5. Nuitka

Nuitka is a Python compiler that converts Python source code into standalone executables, ensuring cross-platform compatibility without a Python interpreter. It simplifies software distribution, eliminates concerns about Python versions and dependencies, and enhances the application’s performance.

First, we install Nuitka using the pip command:

$ pip install nuitka
Collecting nuitka
  Downloading Nuitka-1.8.3.tar.gz (3.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.6/3.6 MB 1.2 MB/s eta 0:00:00
...
... output truncated ...
...

Let’s take a simple test script written using python and executed using Python3:

$ cat baeldung_python_binary_demo.py
#!/usr/bin/python
print ("Welcome to Baeldung Article using nuitka")

$ python3 baeldung_python_binary_demo.py
Welcome to Baeldung Article

Now, let’s use nuitka to compile and convert this Python script into a binary file:

$ nuitka --standalone baeldung_python_binary_demo.py

Here, we find the resulting binary executable in the bin directory within the output folder. This executable runs on various platforms independently.

Thus, Nuitka‘s compilation process optimizes performance and minimizes the executable’s size, making it an efficient solution for distributing Python applications.

Therefore, depending on our needs and target platform, we can choose from tools like PyInstaller, py2exe, or Nuitka to achieve this conversion. Each method has its strengths and may require different configuration options. So, it’s essential to select the one that best fits our project requirements.

6. Conclusion

In conclusion, converting Python scripts into binary executables offers numerous advantages. This article elaborated on the importance of leveraging the tools that aid this conversion. Further, we dealt with the methods to achieve this conversion using PyInstaller, py2exe, and Nuitka. Each method has its configuration options, ensuring the conversion process’s flexibility.