create executable in python

How to Create Executable in Python

Python is a popular programming language used by many developers and organizations. Sometimes you may need to make standalone executable from python script, or create executable with dependencies, or convert python file to exe. This helps make your application portable across Windows platform. There are many python packages to help you accomplish this. In this article, we will learn how to create executable in Python using PyInstaller module.


How to Create Executable in Python

Here are the steps to create executable file in Python.


1. Add Python to PATH

If Python is not added to Windows PATH, then you need to add it first. You can easily do this by downloading a latest installation of Python, and checking the ‘Add Python to PATH’ checkbox at the beginning of installation.


2. Open Command Prompt

Open Windows Command Prompt by clicking on Start Button and typing cmd in searchbox. You will see ‘Command Prompt’ in list of suggestions. Click it to open command prompt in windows.


3. Install PyInstaller

PyInstaller package bundles your Python file along with its dependencies into an executable. Install it with pip command.

$ pip install pyinstaller


4. Create Python Script

If you have not already created python script, you can create one with a sample code below. If you already have a python script you want to convert into executable, then you can skip this step and use your python file in next step.

import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()

def hello ():  
    label1 = tk.Label(root, text= 'Hello World!', fg='green', font=('helvetica', 12, 'bold'))
    canvas1.create_window(150, 200, window=label1)
    
button1 = tk.Button(text='Click Me',command=hello, bg='brown',fg='white')
canvas1.create_window(150, 150, window=button1)

root.mainloop()

For our example, we will save the above code as hello.py at C:\Users\John\Desktop\MyPython.


5. Create Executable using PyInstaller

Now that we have PyInstaller installed and our python script ready, we will proceed with the task of converting it into executable.

Go to the folder which contains your python script hello.py.

C:\>cd C:\Users\John\Desktop\MyPython

Next, run the following command on your python script. Replace hello.py with the name of your python script.

pyinstaller --onefile hello.py

Please remember to use –onefile option to convert it into a single .exe file. Else PyInstaller will create a folder of files.

Basically, when you run the above command, pyinstaller will analyze your code, find out the various libraries and packages required by your code to execute. It will collect the copies of all those modules including your python interpreter and puts them all in a single file, or folder, depending on whether you use –onefile option or not.

If you are really curious about the contents of your pyinstaller output, then create a copy of your python script in another folder, and call pyinstaller in that folder, without –onefile option. You will see all the files & folders that Pyinstaller bundles into the executable.


6. Run Executable

You will find that PyInstaller created .spec file, and 3 folders build, dist and __pycache__ in the same folder where your python script is located. Open dist subfolder. In this folder, you will find the .exe file with the same filename as your python script, that is, hello.exe in our case.

You can double click it to run your Python program. In our case, you will see the following window with a button ‘Click Me’ that you can click to view ‘Hello World’ message. If you get an error message, you may need to install Visual C++ Redistributable. If your python script does not use any windows application like tkinter, then you will see the output in a console window.

It is important to note that PyInstaller will create an executable file and not an installer for your Python file. If you don’t use –onefile option, then Pyinstaller will create a list of folders with all dependencies. Either way, since Pyinstaller will bundle python interpreter and modules required to run your python script, the size of your executable will be much larger than your python script.

Once you have created the executable, it is self-dependent and portable to other windows systems. PyInstaller is a handy tool to easily create executables from python files.

Also read:

How to Store JSON Data in MySQL
How to ‘Fix Dpkg was Interrupted’ Error in Linux
How to POST JSON Data in cURL
How to Store JSON to File in Python
How to Handle Multiple Exceptions in Python

2 thoughts on “How to Create Executable in Python

  1. What if I have not clicked ‘Add Python to PATH’ as 99% of people do by default?
    After command prompt in windows, I do not have $. What does it mean and how to fix?

Leave a Reply

Your email address will not be published. Required fields are marked *