Writing scripts that are based on Python libraries and are meant to be executed more than once is a good practice. It is a good way to ensure that your code is not executing on a system that may not have all the required Python modules installed. Compiling a python script to an executable file is also a good practice. This will ensure that script runs independently of the Python installed on the system. This tutorial will discuss different ways to compile Python scripts to executable files. This includes compiling the script on Linux and windows.

Prerequisite:

For compiling the python script into a .exe file first, we need a Python script here we created a simple python script that gives you simple messes when clicking one button.

import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 299, height = 299, bg='black')
canvas1.pack()

def hello (): 
    label1 = tk.Label(root, text= 'Hello Programmer!', fg='red', font=('helvetica', 12, 'bold'))
    canvas1.create_window(150, 200, window=label1)
 
button1 = tk.Button(text='say hello',command=hello, bg='white',fg='black')
canvas1.create_window(150, 150, window=button1)

root.mainloop()

How to Compile a python script for windows:

We are going to use three python libraries to Compile a python script into an executable file for windows

1.Compiling the script using Pyinstaller library:

Step 1:

To install pyinstaller use the following command in the command prompt.

pip install pyinstaller

Pyinstaller: With PyInstaller, you can bundle your Python application and all of the packages it relies on into a single, compact executable. This reduces the need to install dozens or hundreds of packages when deploying an application or when helping students set up their programming environment. PyInstaller supports Python 2 and above. 

Step 2:

Create a folder and save the python script in it. For example, we created a folder name exe_app and saved my python script named app.py in it.

Step 3:

Open a command prompt in the same folder where our python script is saved and type the following command and press enter.

pyinstaller --onefile app.py

Output:

Step 4:

After running the command you can see that more files and folders are created in the root folder. There you can see folder name dist in that you can find the . EXE file.

You can open the app.exe file to see it running like a normal windows application, you can click on the say hello button that shows the message.

2. Compiling the script using Py2exe library:

Step 1:

To install Py2exe use the following command in the command prompt.

pip install Py2exe

Py2exe: py2exe library is the same as the pyinstaller library.

Step 2:

You need to create a new file setup.py at the same directory where you have the script. This file contains the below parameters. 

from distutils.core import setup
import py2exe
setup(windows=['app.py'])

in place of app.py, you can add your python file name. And if your script runs in the console replace windows for the console.

Step 3:

Open a command prompt in the same folder where our python script is saved type the following command and press enter.

python app.py py2exe

Step 4:

After running the code folder name dist is created in the root folder and in the dist folder, you can find that app.exe is created.

3. Compiling the script using the auto-py-to-exe library:

Step 1:

To install auto-py-to-exe use the following command in the command prompt.

pip install auto-py-to-exe

auto-py-to-exe (GUI Tool): auto-py-to-exe is also a library that converts Python scripts into executable Windows programs, able to run without requiring a Python installation. But it has a graphical interface.

Step 2:

To open the auto-py-to-exe use the following command in the command prompt.

auto-py-to-exe

Output:

As you can see it opens a graphical interface that is already self-explanatory you just have to select the app.py file and select what you want in the given option and press on convert.py to .exe and that’s it. Then it will automatically open the folder of the exe file that you lunch.

How to Compile a python script for Linux:

Step 1:

For Linux, we can use the pyinstaller library as well but for compiling python script for Linux we need to do this process on a Linux system otherwise it will automatically make a .exe file

To install pyinstaller use the following command in the terminal.

pip3 install pyinstaller

Step 2:

Same as before creating a folder and saving the python script in it. For example, we created a folder name exe_app and saved my python script named app.py in it.

Step 3:

Open a terminal in the same folder where our python script is saved type the following command and press enter.

pyinstaller --onefile app.py

Output:

Step 4:

After running the command you can see that more files and folders are created in the root folder. There you can see folder name dist in that you can find the file name app.

you can launch the app file by using the following common in the terminal in the same directory.

./ app

Output:

Conclusion:

It is sometimes necessary to compile (aka build) a python script into an executable file that can be run independently of a python environment. Python does come with its built-in tools for doing so by itself, and in this post, we looked at the many types of the python library that you can easily create a standalone executable program on windows and Linux. for Windows systems, we used pyinstaller and py2exe. We also looked at the auto-py-to-exe, which is a graphical frontend for automatic script compilation. For Linux, we examined pyinstaller. All of these methods are easy to use and if you have any questions don’t hesitate to comment below!

Here are some useful tutorials that you can read: