Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I make an executable from a Python script?
Converting a Python script into a standalone executable allows you to distribute your application without requiring users to install Python. PyInstaller is the most popular tool for this purpose, bundling your script and all dependencies into a single executable file.
Installing PyInstaller
First, install PyInstaller using pip ?
pip install pyinstaller
Sample Python Script
Let's create a simple Tkinter application to demonstrate the conversion process. Save this as demo.py ?
import tkinter
from tkinter import *
# Create main window
top = tkinter.Tk()
top.title("Sports Selection")
# Create checkbox variables
CheckVar1 = IntVar()
CheckVar2 = IntVar()
# Create checkboxes
C1 = Checkbutton(top, text="Cricket", variable=CheckVar1,
onvalue=1, offvalue=0, height=2, width=20)
C2 = Checkbutton(top, text="Football", variable=CheckVar2,
onvalue=1, offvalue=0, height=2, width=20)
# Pack widgets
C1.pack()
C2.pack()
# Start GUI loop
top.mainloop()
Creating the Executable
Open command prompt and navigate to your script's directory, then run ?
pyinstaller --onefile demo.py
This command creates several directories:
- build/ Contains temporary build files
- dist/ Contains your final executable
- demo.spec Configuration file for advanced options
PyInstaller Options
| Option | Purpose | Example |
|---|---|---|
--onefile |
Single executable file | pyinstaller --onefile script.py |
--windowed |
Hide console (GUI apps) | pyinstaller --windowed script.py |
--name |
Custom executable name | pyinstaller --name MyApp script.py |
--icon |
Add custom icon | pyinstaller --icon=app.ico script.py |
Common Use Cases
For GUI applications, combine options for best results ?
pyinstaller --onefile --windowed --name "Sports App" demo.py
For console applications, use basic conversion ?
pyinstaller --onefile console_script.py
Alternative Tools
Other popular Python-to-executable converters include:
- cx_Freeze Cross-platform, good for complex applications
- py2exe Windows-only, lightweight option
- Nuitka Compiles to C++ for better performance
Conclusion
PyInstaller simplifies creating executables from Python scripts with the --onefile option. Use --windowed for GUI applications to hide the console window. The resulting executable in the dist/ folder can be distributed without requiring Python installation.
