This article will discuss how to convert Python files into executables standalone files. When we write a Python program, we require a Python interpreter to execute the code. Also, if we want to run Python code on another machine, there we also have to make sure that it has Python installed on it.
Let's say you have a Python program file, and you want to convert it into a Windows-compatible
executable file so that it can run on any system running Windows without installing Python. How would you do that?
The first thought that comes to the mind of every naive and beginner learner is, "Just rename the
." Unfortunately, this is totally incorrect. Changing the appearance of a dog to a lion won't make the dog a lion.
Likewise, simply renaming the extension of the file won't work. To convert the Python executable code to an OS-specific executable file like, we need a converter, and in this Python tutorial, we will learn how we can use the Python PyInstaller library and command-line tool to convert a Python program to a standalone executable .exe file.
Installing the PyInstaller Library
Here, we will convert two Python programs to equivalent executable .exe files. The first program that we will convert is a console output-based Python program, and the second program is the GUI-based Python Tkinter program, but before we convert the Python program, let's install the Python PyInstaller Library .
To install the same for your Python environment, run the following pip install command on your terminal or command prompt:
pip install PyInstaller
How to Convert Python Files Into Executables Standalone File? [Console-Based]
As we are using Windows for this Python tutorial, we will be converting our Python console-based program to the corresponding
executable file. However, 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 we will be converting is
, which prints "Welcome to techgeekbuzz.com!"
#ConsloeProgram.py import time print("Welcome To TechGeekBuzz.com!") time.sleep(60) #hold the screen for 60 seconds
In the above program, we have used the
function that will hold the output
window for 60 seconds, and once the time runs out, the window will be destroyed 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
command on the terminal or command prompt.
pyinstaller --onefile --console ConsloeProgram.py
In the above command, we have also used two flags, namely
flag will create a single executable bundled file, while the
flag will open the console window for I/O.
When you run the above terminal command on your command prompt or PowerShell or terminal, some new directories and files will be created in the directory having your Python script.
Also, you will find the executable
program in the
And when you double-click on the
file, a new window will pop up with the program output.
How to Convert Python Files Into Executables Standalone File? [GUI-Based]
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
file so it can run on any Windows system without installing Python. The following Python program will help you do so:
#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
terminal command followed by the required flags and filename
pyinstaller --onefile --noconsole GuiPython.py
flag will create a single bundle of the executable file. The
flag will not open the extra console window for the standard i/o. Therefore, you should use the
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
file in the newly created
To execute the
, double-click on the file.
In this Python tutorial, we learned how to convert Python files into executables standalone files, console-based and GUI-based. The PyInstaller library is available for all platforms that support Python. Using this powerful Python command-line utility, you can convert a Python program to an executable file that can run on any compatible operating system.
People are also reading:
- Slice List/Arrays and Tuples in Python
- How to print without newline in Python?
- Invalid Syntax Python
- Python Remove Empty string from a List of Strings
- Round to whole numbers in Python
- Python Exponent
- Enumerate In Python
- Convert a List to Dictionary in Python
- Bottle Framework in Python
- Python Pyramid Framework
Leave a Comment on this Post