How to convert .py to .exe
In this tutorial, we’ll be learning how we can create an executable file for our Python program that can run on any same platform system. We will make use of Python library PyInstaller which is used specifically for this purpose. This .exe file does not require any command or a Python interpreter to run. So let’s start learning how to convert .py file to .exe using PyInstaller.
To get started we need to install pyinstaller using the following pip command.
pip install pyinstaller
If this does not work, check official PyInstaller documentation here
How to use PyInstaller
Let us create a simple program that adds two numbers a and b and run our program.
a = 10 b = 5 print(a+b)
We will get the output as 15, for that we have to type the command in Command-Line Input(CLI) with support for Python interpreter. We have named our file as addition.py.
But we want to make a .exe file, for that follow the steps below.
Convert .py to .exe using PyInstaller
STEP 1. Open CLI and change directory to the directory having your .py file.
STEP 2. Then type the command in the format given:
*Wait until the completion of command as it generally takes time.*
STEP 3. After the completion, we will get two folders ‘Build’ & ‘Dist’ and one .spec file in our working directory.
The only folder we are interested in is the Dist so open Dist folder, there we will get a directory whose name will be the same as our filename.
STEP 4. Open the folder and DONE!. It will have many files including our filename.exe.
Open the file and our program will run automatically.
Important Points: Creating .exe from .py file
- We can run more complex programs using external or internal python libraries including the one for GUIs.
- We can run this .exe file on any Computer having the same OS platform.
- If your program includes images, videos or any other external file then make sure you move the .exe file from dist to the directory having all such dependencies.
That’s all for this tutorial. I hope you like this tutorial and if you have any doubt feel free to leave a comment below.
You may also Learn