In this video it shows the steps to create an EXE (windows executable) file for your Python Application. This EXE file can be used to be distributed to all the Windows OS users. They can run this as their desktop application.
For creating the EXE it uses the pyinstaller command. The complete command used is as follows:
pyinstaller --onefile --windowed .\SimpleCalculator.py
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Command needed:
To install PYINSTALLER
pip install pyinstaller
To create EXE file
pyinstaller --onefile --windowed .\SimpleCalculator.py
Output from the Terminal window:
PS C:\Work\PythonCalculator> pyinstaller --onefile --windowed .\SimpleCalculator.py
351 INFO: PyInstaller: 6.11.0, contrib hooks: 2024.9
351 INFO: Python: 3.12.7
374 INFO: Platform: Windows-11-10.0.26100-SP0
374 INFO: Python environment: C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.2032.0_x64__qbz5n2kfra8p0
375 INFO: wrote C:\Work\PythonCalculator\SimpleCalculator.spec
394 INFO: Module search paths (PYTHONPATH):
['C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\Scripts\\pyinstaller.exe',
'C:\\Program '
'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.2032.0_x64__qbz5n2kfra8p0\\python312.zip',
'C:\\Program '
'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.2032.0_x64__qbz5n2kfra8p0\\DLLs',
'C:\\Program '
'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.2032.0_x64__qbz5n2kfra8p0\\Lib',
'C:\\Program '
'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.2032.0_x64__qbz5n2kfra8p0',
'C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages',
'C:\\Program '
'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.2032.0_x64__qbz5n2kfra8p0\\Lib\\site-packages',
'C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\setuptools\\_vendor',
'C:\\Work\\PythonCalculator']
1146 INFO: checking Analysis
1147 INFO: Building Analysis because Analysis-00.toc is non existent
1147 INFO: Running Analysis Analysis-00.toc
1147 INFO: Target bytecode optimization level: 0
1148 INFO: Initializing module dependency graph...
1150 INFO: Initializing module graph hook caches...
1225 INFO: Analyzing base_library.zip ...
3269 INFO: Processing standard module hook 'hook-encodings.py' from 'C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\PyInstaller\\hooks'
4233 INFO: Processing standard module hook 'hook-heapq.py' from 'C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\PyInstaller\\hooks'
5525 INFO: Processing standard module hook 'hook-pickle.py' from 'C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\PyInstaller\\hooks'
7300 INFO: Caching module dependency graph...
7510 INFO: Looking for Python shared library...
7511 INFO: Using Python shared library: C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.2032.0_x64__qbz5n2kfra8p0\python312.dll
7511 INFO: Analyzing C:\Work\PythonCalculator\SimpleCalculator.py
7643 INFO: Processing standard module hook 'hook-_tkinter.py' from 'C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\PyInstaller\\hooks'
7679 INFO: Processing module hooks (post-graph stage)...
7682 INFO: Processing standard module hook 'hook-_tkinter.py' from 'C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\PyInstaller\\hooks'
7683 INFO: TclTkInfo: initializing cached Tcl/Tk info...
7864 INFO: Performing binary vs. data reclassification (925 entries)
8269 INFO: Looking for ctypes DLLs
8276 INFO: Analyzing run-time hooks ...
8277 INFO: Including run-time hook 'pyi_rth_inspect.py' from 'C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\PyInstaller\\hooks\\rthooks'
8293 INFO: Including run-time hook 'pyi_rth__tkinter.py' from 'C:\\Users\\abhis\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\PyInstaller\\hooks\\rthooks'
8336 INFO: Looking for dynamic libraries
8533 INFO: Extra DLL search directories (AddDllDirectory): []
8534 INFO: Extra DLL search directories (PATH): []
8996 INFO: Warnings written to C:\Work\PythonCalculator\build\SimpleCalculator\warn-SimpleCalculator.txt
9020 INFO: Graph cross-reference written to C:\Work\PythonCalculator\build\SimpleCalculator\xref-SimpleCalculator.html
9129 INFO: checking PYZ
9129 INFO: Building PYZ because PYZ-00.toc is non existent
9131 INFO: Building PYZ (ZlibArchive) C:\Work\PythonCalculator\build\SimpleCalculator\PYZ-00.pyz
9564 INFO: Building PYZ (ZlibArchive) C:\Work\PythonCalculator\build\SimpleCalculator\PYZ-00.pyz completed successfully.
9601 INFO: checking PKG
9602 INFO: Building PKG because PKG-00.toc is non existent
9602 INFO: Building PKG (CArchive) SimpleCalculator.pkg
13837 INFO: Building PKG (CArchive) SimpleCalculator.pkg completed successfully.
13861 INFO: Bootloader C:\Users\abhis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\PyInstaller\bootloader\Windows-64bit-intel\runw.exe
13862 INFO: checking EXE
13862 INFO: Building EXE because EXE-00.toc is non existent
13862 INFO: Building EXE from EXE-00.toc
13863 INFO: Copying bootloader EXE to C:\Work\PythonCalculator\dist\SimpleCalculator.exe
13948 INFO: Copying icon to EXE
14001 INFO: Copying 0 resources to EXE
14001 INFO: Embedding manifest in EXE
14047 INFO: Appending PKG archive to EXE
14062 INFO: Fixing EXE headers
14277 INFO: Building EXE from EXE-00.toc completed successfully.
PS C:\Work\PythonCalculator>
Code used in this demo:
import tkinter as tk
from tkinter import messagebox
# Create the main window
root = tk.Tk()
root.title(“Simple Calculator”)
root.geometry(“500×500”)
# Global variable for storing the expression
expression = “”
# Function to update the expression in the entry widget
def press(num):
global expression
expression += str(num)
equation.set(expression)
# Function to evaluate the final expression
def equalpress():
try:
global expression
total = str(eval(expression))
equation.set(total)
expression = total
except:
equation.set(“Error”)
expression = “”
# Function to clear the expression in the entry widget
def clear():
global expression
expression = “”
equation.set(“”)
# Entry field for showing expression/result
equation = tk.StringVar()
expression_field = tk.Entry(root, textvariable=equation, font=(“Arial”, 20), bd=10, insertwidth=4, width=25, borderwidth=4)
expression_field.grid(row=0, column=0, columnspan=4)
# Button layout
buttons = [
(‘7’, 1, 0), (‘8’, 1, 1), (‘9’, 1, 2), (‘+’, 1, 3),
(‘4’, 2, 0), (‘5’, 2, 1), (‘6’, 2, 2), (‘-‘, 2, 3),
(‘1’, 3, 0), (‘2’, 3, 1), (‘3’, 3, 2), (‘*’, 3, 3),
(‘C’, 4, 0), (‘0’, 4, 1), (‘=’, 4, 2), (‘/’, 4, 3),
]
# Create buttons using a loop
for (text, row, col) in buttons:
if text == ‘=’:
btn = tk.Button(root, text=text, height=3, width=8, font=(“Arial”, 18), command=equalpress)
elif text == ‘C’:
btn = tk.Button(root, text=text, height=3, width=8, font=(“Arial”, 18), command=clear)
else:
btn = tk.Button(root, text=text, height=3, width=8, font=(“Arial”, 18), command=lambda txt=text: press(txt))
btn.grid(row=row, column=col)
# Start the GUI loop
root.mainloop()
Screenshots:
Path: C:\Work\PythonCalculator\dist