Converting .py to .exe
Today we will discuss the process of converting a python, i.e., the .py file, into an executable or .exe file. Let us start with the introduction of the .exe file. In windows, .exe is an extension used for executable files. It is one of the most common file extensions used for almost all kinds of software and applications programs and setups.
Now let’s come to the purpose of the conversion. We create lots of Python programs and want to share them with the world. If we want our python program to run on any computer without the IDE or even installing Python itself, we must convert it to .exe. All the apps and programs we use on our computer are written using some language or code, but we do not have to install the particular language for the program to run.
Steps to Create an Executable from Python Script using Pyinstaller
Step 1: Install the Pyinstaller Package
The first step is to install the module pyinstaller. For this, we will create or destinate a folder and open our power shell window there using shift + mouse right-click.
Now we will run the following command for our module installation.
Step 2: Save your Python Script
Right-click on the screen and then new and text document to create a .txt file and write the python script. We can save our python script in a text file, too. For the conversion, it does not necessarily have to be a .py file, but the code should be Python. Now open the power shell window and write the following commands.
The code written in the main.txt file will execute.
Step 3: Create the Executable using Pyinstaller
As we want a .exe file. So we are going to run the following command.
It will show us a few warnings, as shown in the image below. Along with that, it could be a little time-consuming in the case of bigger programs. We should not avoid the warnings as they can be a security threat later on. You can search for the meaning of the given warning by searching it on the internet. Sometimes software causes problems while installing the pyinstaller module. The reason for this might be the incomplete installation of pip. So for that check, pip is not recognized error solution video for the solution.
Step 4: Run the Executable
After running the pyinstaller main.txt command, it will create some folders. Click on the dist folder and then click on the main.
In that folder, we can find our .exe file. We can open it through the PowerShell window by running the command.
Now, as you saw, by converting the file to .exe, we got several files in the folder, but we can also run a command that will provide us with only one file as a resultant. It will take more time in creation, and later on, it will extract too, but for compatibility, we can use the following command:
Here the file extension depends on your file that whether you created it using .py or .txt.
Once we click on the file, we are ready to launch our program. So, that’s how you can easily convert the .py file to the .exe file in Python. This tutorial ends here, and I will see you in the next tutorial.
Comments
Post a Comment