How to Create Executable of Python Script Using PyInstaller?

In Python, “Pyinstaller” package is used to create the “executable” of any Python script. An executable file is used to launch any application which works on the sequence of instructions. The “Pyinstaller” package can be obtained from the Python Package Manager named, “PIP”. 

This post will address the possible methods to create an executable script using the PyInstaller. 

How to Create an Executable Python Script using PyInstaller?

Before going into the ” Pyinstaller ” installation process, we need to add Python to the windows path. Because if Python is not added to the windows path, then the “pip” command will not work properly. To add Python to the window path, you can read this guide

Install PyInstaller Package in Python

To install the “Pyinstaller” package in Python; you need to open the cmd terminal and type the following command:

pip install pyinstaller

The below snippet demonstrates the successful installation of “Pyinstaller” on our system.

Example: Creating Executable of Python Using PyInstaller

To make an executable of Python script, we need to create the “script” in Python using the “tkinter” package. If you don’t have the Tkinter module, then you must read this guide

In the below code, the tkinter module is used to create a simple Python script that will print “Python Guide by itslinuxfoss” when executed:

Code:

from tkinter import *
application = Tk()
application.title("mypython_app")
application.geometry("600x300")
output = Label(application, text="Python Guide by itslinuxfoss", fg='blue', font=('helvetica', 12, 'bold'))
output.configure(anchor=CENTER)
output.pack()
application.mainloop()

In the above code, the simple Python script named “mypython_app” is created, which shows the given “Text” when executed.

After the creation of the Python script, we now need to make an executable file of this Python script. To create an executable file follow the below steps:

Step 1: Open CMD and Change the Directory

In the first step, open the “command prompt terminal” and change the directory using the “cd” command followed by the directory’s path where you saved the “Python script” file.

In our case, the path of the selected Python script is shown below:

cd C:\Users\p\Documents\program

The above command will direct you to the specified path; for instance, here is an example:

Step 2: Create Executable FIle

After moving into the Python script directory in cmd, the next step is to create an executable file using the “Pyinstaller” package. Type the given command along with the name of your Python script in cmd to create an executable file:

pyinstaller --onefile mypython_app.py

In the above command, “mypython_app.py” represents the name of the executable file to be created. 

Now we type the above command in cmd and press enter:

The below snippet shows that the “executable” file has been successfully created using the Pyinstaller module:

Step 3: Open dist Folder

Now navigate to the “Python script” directory and open the “dist” folder.

Step 4: Open Executable Python File

In the “dist” directory, the “executable” file of our Python script is placed with the same name. Double-click the “executable” file to open it.

Output:

The above output shows that our Python executable file has been successfully created. The “Text” which was initialized inside the Python script is shown on the output screen.

Note: Hiding Consoler

As you can see the executable file also opens the consoler in the background. To hide the consoler, you need to run the below command in the command prompt: 

pyinstaller --noconsole --onefile filename.py

Replace filename.py with your script file name.

Conclusion

To create an executable or “.exe” file of Python script the “Pyinstaller” package is used in Python via the command prompt terminal. The tkinter module creates a simple Python script that shows the “Text” on the screen when executed. This guide provides a complete process of creating an executable Python script using numerous examples.