Convert Python Program to exe

Rishabh Singh
7 min readJul 24, 2022

--

In this article, we are going to learn how to convert Python to exe file. We all know that it’s very simple to create GUI applications with Python like using the Tkinter library in Python. But, what if you want to share your application with someone who doesn’t know how to run Python. The answer is that you will ask him to watch online blogs for setting-up Python in their system and then run it.

But wait, we have very simple 2 methods using which you can convert your Python application to exe files and share them directly with anyone. So, let’s start to learn how to convert Python to exe file.

.py to .exe

Methods to Convert py to exe file

  1. Using pyinstaller
  2. Using auto-py-to-exe

1. Using pyinstaller to compile Python to exe

Basic Overview of PyInstaller tutorial

In this first method to convert .py to .exe file, a Python program and all of its dependencies are combined into one package by PyInstaller. The user does not need to install any modules or a Python interpreter in order to launch the packed program. By introspecting your Python code, identifying your dependencies, and then packaging them into the appropriate format based on your Operating System, PyInstaller accomplishes this astounding accomplishment.

There are many fascinating subtleties about PyInstaller, but for the time being, we’ll only cover the fundamentals of how it functions and how to utilize it. Additionally, PyInstaller may produce Windows, Linux, or macOS executables. Accordingly, Linux users will receive a standard executable, Mac users a bundle of .app files, and Windows users a .exe.

Steps

Step 1: Installation of pyinstaller using the following command:

pip install pyinstaller

Step 2: Now open the folder where your py file is stored and open that folder in terminal

There are other ways of doing this but the best one is to open your folder in the terminal. This will help you to not worry about copying and pasting the path where your script is stored. Here in our case, our attendance_management.py file is stored in a folder named attendance_management. So we opened that folder. Once that folder is opened, right-click and select the option Open in Terminal.

Open terminal on win11

Step 3: Now paste the below command in your Terminal/Windows Power Shell

pyinstaller --onefile -w 'filename.py'

Now once you paste this command into your terminal and hit enter, a message will be displayed in your terminal notifying you that the exe file is created. Please note that you need to replace ‘filename.py’ with your file name (python file) that you want to convert to exe.

Step 4: Check your folder

Once you run that command, come back to your folder and you will see that there are two new folders namely build and dist, and one spec file is created. You might get confused by seeing these folders that where is the exe file. You can find your exe file in the dist folder. The explanation of each folder is further explained along with their purpose.

Output:

Output for Pyinstaller

Step 5: Open the dist folder and there’s your exe file

Now once you open this dist folder you will see the exe file that you can share with others without the need for source code. But you might have questions that why there is a build folder and one spec file and what are they responsible for. As far as these folders are concerned then they are of not that much use to beginners but if you want advanced settings then you can modify them accordingly and use it. A brief description of created folders is as below:

  • dist folder: The last artefact you should distribute to your users is located in the dist/ subdirectory. You can find a file with the name of your entry point inside the dist folder. As a result, your executable file in this example will be called attendance_management.exe.
  • build folder: The majority of the metadata and internal recordkeeping for creating your executable is stored by PyInstaller in the build subdirectory. The build folder can be helpful for troubleshooting, but unless there are issues, you can generally ignore this folder.
  • spec file: Knowing what to concentrate on first is crucial. Specifically, the executable you can make available to your users and any potential debugging data. The pyinstaller command by default will produce the following useful items:

Finally, we have converted our py file to exe using python pyinstaller. This was the very first method to create exe from python file. Although this method is quite simply the best one is yet to come. Go on reading and find out which one is best for you.

2. Using auto-py-to-exe to create exe from python

Basic Overview for auto-py-to-exe

Another easy and unique method to convert python to exe file is a Python utility called Auto-py-to-exe which can easily transform a Python.py file into an executable that has all of its dependencies packed. Another advantage is that Auto-py-to-exe creates an executable file that is a built version of the source code rather than the original source code. This makes it harder for others to steal your code.

Here, in this section of the article, “convert python to exe,” we’ll go over all there is to know about auto-py-to-exe, from how to install it to how to use it.

Steps

Step 1: Installation of auto-py-to-exe using the below command:

pip install auto-py-to-exe

Step 2: Now to run auto-py-to-exe, use the following command:

auto-py-to-exe

Once you run the above command, a GUI will appear on your screen and that will help you to convert the py file to exe. This GUI offers many options like selecting the path, options like one directory or one file are our application windows or console-based, etc. We will go through each option in detail. The output will be as shown below:

Output:

auto-py-to-exe GUI .py to .exe converter

Step 3: Pick the .py file that you want to convert to .exe

Here we will convert our Attendance Management System project python file to exe file. In this step basically, you have to select the file that you want to convert using the Browse button. The path in the blue box shows the path of your selected file.

Output:

auto-py-to-exe selecting path

Step 4: What to choose One File or One Directory?

One File: If “One File” is selected, “Auto PY to EXE” will produce a single .exe file that contains all dependencies but NOT media files. The following explanation can be skipped if your software solely uses the standard Windows GUI and doesn’t have any icons, backdrops, or media files, or if you’re fine with placing a media folder close to an executable file. You should read Step 5 if you want to pack media files directly into a .exe file.

One Directory: The “Auto PY to EXE” option, when set to “One Directory,” will place all dependencies in a single location. The “Advanced” menu’s “Output directory” option is available. If you put media files or folders in the Output directory, using media assets like icons and backdrops inside your .exe shouldn’t be a problem.

Step 5: Select Additional Files

You can add whatever files you want using the “Additional Files” menu in “Auto PY to EXE.” But there’s a catch. Pyinstaller is used by “Auto PY to EXE” to unpack data into a temporary folder and store the directory path in the _MEIPASS environment variable. Because the path has changed, your project won’t be able to locate the required files and won’t recognize the new path either. In other words, the selected files from the “Additional Files” menu will not be included in the .exe file if the option “One File” is selected.

Step 6: Run your program by clicking on CONVERT .PY TO .EXE

Now once you hit on that button, you might see some processes happening in the Output section. Once your file is converted (might take some time based on the size of your project) you will see two buttons at the bottom and a message as shown in the Red box will display a completion message with a path where your .exe file is saved. By clicking on the OPEN OUTPUT FOLDER, it will take you to the folder where your exe file is saved and another button CLEAR OUTPUT will let you select another project that you want to convert.

Output:

auto-py-t-exe final output

--

--