Sometimes we want to share our created project with people so that they can see and use it. But, not everyone knows to run the program, and they do not know how to run the project. To solve this problem developers, have one option to convert their coded file into an executable file using PyInstaller package. Because an executable file is easy to use, and there is no difficulty in executing it. Anyone can open and operate an executable file. For example, you have built a game in Python programming, and you want to share it with your family and friends, but you cannot share your Python code file directly because everyone does not know how to open it and run it. So, to solve this difficulty, we write this tutorial on converting a python code file into an executable file to help developers make their project accessible for all.
Table of Contents
Why convert a py file into an exe file?
Because an executable file does not need Python installation to run it, a person can execute it by double-clicking on the icon. The people who have zero knowledge of programming can run it efficiently.
To convert a py file into an exe file first, we need to create a project in Python Programming and then convert that script into a single executable file. You will require PyInstaller to perform this conversion; you need to install it using the pip command. Let’s see the whole process below in this article.
PyInstaller is one of the popular packages of Python that bundles or groups a Python application and all its dependencies together in a single package or a single file. It collects all Python files, including active Python interpreter, and wraps up or puts them into a single folder, or you can say in an executable file. PyInstaller package includes standard libraries NumPy, QT, and more.
Command to install PyInstaller is: pip install pyinstaller
Conversion py to exe
There are some steps to perform this conversion.
Step# 01: Install PyInstaller
To install the package pyinstaller, go to your command prompt (CMD) and run the “pip install pyinstaller” command.
If you are using an IDE with its terminal, you can run this command in that terminal. For example, if you use PyCharm IDE or any other IDE with its terminal, you can run “pip install pyinstaller” in its local terminal.
In PyCharm IDE, you can install packages manually. First, go to the settings section of IDE.
Settings > Project: Your Project
Settings > Project: Your Project > Project Interpreter
Click on the plus (+) sign.
Search for pyinstaller package
Here it is installed now.
Step# 02: Create your project/ Build your Application
I have already created a project that is a text editor application. You can create any project of your choice and make it executable— I have created a Python GUI project using the Tkinter library. If you want to see my project, you can download the MyEditor. The project’s important material or resource files, like python source code file, icon file, etc., are provided in the zip file.
Step# 03: Make your file executable
To make your Python file an executable file, you need to run the “pyinstaller –onefile YourProject.py” command in the terminal as my project name is VpadTextEditor.py so that I will run the “pyinstaller –onefile VpadTextEditor.py” command in my terminal. It will create three main files or directories build, dist, and project.spec is a spec file. The exe file will exist in the dist folder. Before running the command, make sure that you are in the targeted project directory.
Hence it is successfully converted. Now go to the project directory.
Exe file exists in dist directory so go to the dist directory.
Here is your exe file. Run your executable file.
In conclusion, we saw the process of converting a Python file into an executable file. The conversion py to exe includes some steps to be performed. That was the installation of pyinstaller, project creation, and making the project executable. We need to make our project/application executable by every person with or without knowledge of Python programming.