How to Create Exe File for Python Program using Pyinstaller

What if you have a single exe file that can be run without a python interpreter? If you have these questions, then you are at the right place. Today, we will create an exe file for a python program using Pyinstaller.

How to Run a Python Script from PHP

Note: Exe files can be created on any platform but can only be run in windows based system.

Step 1: Install and Add Python to Windows Path

You might have already installed python in your system. But for those who don’t, you can download it from the official website.

While installing, make sure to click on the checkbox with the message “Add python* to PATH”.

Python Installer
Python Installer

This will allow us to access python and PIP directly from the command prompt.

Step 2: Install the Pyinstaller Package

Next, you have to open a command prompt using the search bar and type CMD or the WIN+R shortcut key to open the Run application and type CMD.

Note: For the Linux family, you can have this package in your system to create an exe file. But that file can only be run in Windows.

Once your command prompt is ready, you can copy or write the below command to install Pyinstaller in your system.

pip install pyinstaller

Step 3: Create Python Program

Now only one main ingredient is left in your system before creating an exe file—a python program, which we will use in this experiment.

I already created a basic python program, not too fancy. Its role is to display “Pyinstaller Guide” in Tkinter Window. In your case, it can be a complex program written in NumPy, PyQT, wxPython, etc.

from tkinter import *

app = Tk()

app.title("TREND OCEANS")
app.geometry("500x200")
l = Label(app, text="Pyinstaller Guide", font=("Arial Bold", 20))
l.configure(anchor=CENTER)
l.pack()

app.mainloop()

For instance, you can use the above code and save it with the “.py” extension. Personal suggestion, while saving your file, make sure the directory is empty. So, when we create an exe file, it can be easily spotted.

Step 4: Create the Exe File using Pyinstaller

I think your command prompt is still open from step 2. I recommend closing it, or if you are a tech geek, you can directly navigate to the location where you have saved your python program from step 3.

Otherwise, navigate to file location from the file explorer click and type cmd in the address bar like shown below.

File explorer
File explorer

Finally, we can execute the pyinstaller command to generate an exe file from our python program. Below is the syntax of the command we will use.

Syntax

pyinstaller --onefile [filename.py]

If you use a custom program, replace the filename.py with [yourfilename].py. In my case, it’s script.py.

pyinstaller --onefile script.py
Create Exe File for Python Program using Pyinstaller
Create Exe File for Python Program using Pyinstaller

After you execute the above command, it will create a few files & folders in the current directory. You have to enter in the dist directory.

Navigating to dist folder
Navigating to dist folder

Once you enter the directory, only a single file will be there with the same name of your file but with a “.exe” extension.

script.exe
script.exe

You can quickly run this application from the context menu or double-clicking on the exe file.

Python Program with Console
Python Program with Console

While your application is running behind the scene, you will also spot your console is also running. To hide that console whenever you run your application pass –noconsole option while generating exe file.

pyinstaller --noconsole --onefile script.py
Python Program
Python Program without Console

If you have any issues while creating exe files from the python program. Let us know in the comment section so that we can guide you.

This Post Has 4 Comments

  1. Iancu Ioan

    How can I create a shortcut to Desktop from inside the python script so after I made the .exe and send by email to a user shortcut appear on Desktop. Thanks.

    1. Jake Redfield

      That’s a pretty nice question, you can create shortcuts automatically. But you have to make some changes in your python script.
      Below is the boiler template you can use and edit your script according to that.

      import win32com.client
      import pythoncom
      import os
      desktop = r'C:\Users\Public\Desktop' # Default location to create the .lnk file is desktop (Meaning shortcut icon) but you can change it to something else.
      path = os.path.join(desktop, 'NameOfShortcut.lnk') # Name of your shortcut icon (You can change it depending upon your filename but don't replace extension)
      target = r'D:\Junk\asd.txt' # Location of file which will be triggered when people click on shortcut icon.
      icon = r'D:\Junk\180862.ico' # Icon for your shotcut icon, make sure it is .ico image

      shell = win32com.client.Dispatch("WScript.Shell")
      shortcut = shell.CreateShortCut(path)
      shortcut.Targetpath = target
      shortcut.IconLocation = icon
      shortcut.WindowStyle = 7 # 7 - Minimized, 3 - Maximized, 1 - Normal
      shortcut.save()

      Also, make sure to install win32 python library using pip install pywin32 from your command prompt.
      And also your shortcut icon will only appear when the user clicks on the actual exe for the first time.

  2. Iancu Ioan

    Thanks Jake Redfield, your answer was very helpfull. Yes, the shortcut is created after the first run of the .exe. Is there any posibility to create the shortcut when users download compresed(archived) .exe from email or from a link? Thank you very much.

    1. Jake Redfield

      Yes, you can create a setup file using the Winrar SFX option.

Leave a Reply