Are you working on Python Project. You want to make python executable files instead of running script every time. If yes, you are in the perfect place. In this tutorial, we will introduce you to the pyinstaller. It can help you make executable binary files for your project.
We will also guide you about compiling one file and multiple file projects using pyinstaller in this tutorial. So, keep reading this guide to compile your python projects now.
What is PyInstaller?
PyInstaller is a program that converts Python scripts into python executable file. It is a popular tool for packaging Python applications in a binary so that it can be distributed and run on other computers without requiring the other users to have Python installed.
PyInstaller has many benefits such as
- It generates binary file so you don’t have to run script again and again using python3 command.
- It will run even if you don’t have python3 in the PC where you want to run the application
First of all, install the Pyinstaller using the following command
sudo pip install pyinstaller
Making Binary from Python script
Open a command prompt/terminal and go to your python file location. After that, run the following command
Now, the binary file for your python script is ready in dist folder for a single file project. For multifile python project, continue reading!
In the folder, where your python file is present, a new dist directory will create. Go to the dist directory and run the python executable
cd dist/ ./python_filename
Python Executable for Multiple File Project
If your python project has multiple files, then building python excutable is a difficult. But don’t worry, we will explain you with examples here.
In the screenshot, you can see a tree of our multiple file project
In the above screenshot, Python project is used to create the HTTP server which uses HTML, CSS, and a shell script to make a server. Here our main file is http_server.py and when we try to build it using pyinstaller http_server.py, it gives the error shown in figure below
So, we have to incorporate the dependent files in pyinstaller command somehow. Isn’t it. So, here is the updated command we used to build our multiple file project.
pyinstaller --onefile --add-data "templates:templates" --add-data "static:static" http_server.py
- –onefile: This option tells PyInstaller to package the application into a single standalone executable file. This means that all the necessary files and dependencies will be bundled into a single file for easier distribution.
- –add-data “templates:templates”: This option specifies additional data files that should be included when building the executable. In this case, it’s instructing compiler to include files from the templates folder.
- –add-data “static:static”: Similar to the previous option, it tells pyinstaller to include files in static folder.
- http_server.py: This is the main Python script of the application that we want to package.
So, using the above command, we incorporated all the dependencies required by http_server.py and it compiled successfully.
If you don’t want to use such big command to compile python script again and again, you can use makefiles to build your projects. Makefile is an amazing tool for compiling applications with multiple command line parameters.