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”.
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()
l = Label(app, text="Pyinstaller Guide", font=("Arial Bold", 20))
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.
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.
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
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.
Once you enter the directory, only a single file will be there with the same name of your file but with a “.exe” extension.
You can quickly run this application from the context menu or double-clicking on the exe file.
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
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.
Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.