When we write a Python program we require a Python interpreter to execute our code, and if we want to execute our code on another machine there we also have to make sure that it has Python installed in it.
Let’s say you have a Python program file
.py and you want to convert that file into windows
.exe executable file, so it can run on any window system without installing Python. So how would you do that? the first thought that comes to every naive and beginner learner mind is, “Just rename the
.py file extension to
.exe“, which is totally incorrect, by changing the appearance of a dog to lion won’t make it a lion.
To convert the Python executable code to a specific operating system executable file like
.exe we need a converter, and in this Python tutorial, we will learn how can we use the Python PyIntaller library cum command-line tool utility to convert our Python program to a stand-alone Executable .exe file.
In this tutorial, we will convert two Python programs to the executable .exe file. The first program that we will convert is a console output-based Python program, and the second program is the GUI-based Python TK inter program.
Before we convert the Program let’s install the Python Pyinstaller Library
To install the PyInstaller Library for your Python environment run the following pip install command on your terminal.
pip install PyInstaller
Convert a Console Based Python Program to Standalone Application
I am on the windows system, so I will be converting my Python console-based program to the corresponding
.exe executable file. If you are on macOS or Linux your Python program will be converted to your corresponding Operating system executable file.
The console-based Python program that I will be converting is
ConsloeProgram.py, that prints “Welcome to techgeekbuzz.com”.
ConsloeProgram.pyimport time print("Welcome To TechGeekBuzz.com") time.sleep(60) #hold the screen for 60 seconds
In the above program, I have used the
sleep(60) function that will hold the output
.exe window for 60 seconds and after it, the window will destroy automatically.
Now It’s time to convert the above Python program to an executable.exe file. To convert the Python program file to a standalone executable file we execute the
pyinstaller pythonfile_name.py command on the terminal or command prompt.
pyinstaller --onefile --console ConsloeProgram.py
In the above command, I have also used two flags
--onefile will create a single executable bundled file.
--console will open the console window for I/O.
When you will run the above terminal command on your command prompt or PowerShell or terminal. Some new directories and files will be created in your Python Script directory.
And you will find the executable
ConsloeProgram.exe program in the dist directory.
And when you will double click on the
ConsloeProgram.exe file a new window will pop up with the program output.
Convert a GUI Based Python Program to Standalone Application
Now let’s say you have created a GUI based Python program using TkInter, and you want to convert the Python program to the corresponding executable
.exe file so it could run on any Windows system without installing Python.
#GuiPython.py from tkinter import * root = Tk() root.geometry("800x800") root.title("TechGeekBuzz App") root.configure(bg="#49A") Label(root, text="WelCome to TechGeekBuzz", font="poppins").pack(pady=10) root.mainloop()
Now run the
pyinstaller terminal command followed by the required flags and filename
pyinstaller --onefile --noconsole GuiPython.py
--onefile flag will create a single bundle of the executable file.
--noconsole flag will not open the extra console window for the standard i/o. You should use
--noconsole flag when you are converting the Python GUI program to an executable program.
After executing the Pyinstaller terminal command you can look for the executable file
.exe file in the newly created dist directory.
To execute the
GuiPython.exe just double click on the file.
In this Python tutorial, we learned how can we convert a Python file to an executable standalone application. The PyIntaller library is available for all the platforms that support Python, using this powerful Python command-line utility you can convert your Python program to an executable file, that can run on any similar operating system.
To know more about how PyIntaller library you can read its official documentation.