- Add build_exe.py script for easy executable generation - Add PYINSTALLER_GUIDE.md with comprehensive instructions - Updated label_printer.spec for PyInstaller configuration - Tested build process - executable builds successfully - Created dist/LabelPrinter with all dependencies bundled - File size: ~200MB (includes Python runtime and all libraries) - No Python installation required to run the exe
5.2 KiB
Building a Standalone EXE with PyInstaller
This guide explains how to create a standalone Windows executable (.exe) file that doesn't require Python to be installed.
Quick Start (Windows)
Prerequisites
- Python 3.11+ installed
- Virtual environment activated
- All dependencies installed
One-Command Build
# Activate virtual environment
venv\Scripts\activate
# Build the executable
python build_exe.py
That's it! Your executable will be in dist/LabelPrinter.exe
What Happens During Build
- Analyzes your code - Finds all imported modules
- Collects dependencies - Bundles Kivy, PIL, barcode, reportlab, etc.
- Creates executable - Packages everything into one
.exefile - Output:
dist/LabelPrinter.exe(~150-200 MB)
Detailed Build Instructions
Step 1: Install PyInstaller
venv\Scripts\activate
pip install pyinstaller
Step 2: Build Using Script
python build_exe.py
Step 3: Alternative Manual Build
If the script doesn't work, use this command directly:
pyinstaller ^
--onefile ^
--windowed ^
--name=LabelPrinter ^
--hidden-import=kivy ^
--hidden-import=kivy.core.window ^
--hidden-import=kivy.core.text ^
--hidden-import=kivy.core.image ^
--hidden-import=kivy.uix.boxlayout ^
--hidden-import=kivy.uix.gridlayout ^
--hidden-import=kivy.uix.label ^
--hidden-import=kivy.uix.textinput ^
--hidden-import=kivy.uix.button ^
--hidden-import=kivy.uix.spinner ^
--hidden-import=kivy.uix.scrollview ^
--hidden-import=kivy.uix.popup ^
--hidden-import=kivy.clock ^
--hidden-import=kivy.graphics ^
--hidden-import=PIL ^
--hidden-import=barcode ^
--hidden-import=reportlab ^
--hidden-import=print_label ^
--hidden-import=print_label_pdf ^
--collect-all=kivy ^
--collect-all=PIL ^
label_printer_gui.py
Output Files
After building, you'll have:
Label-design/
├── dist/
│ └── LabelPrinter.exe ← Your standalone executable (150-200 MB)
├── build/ ← Temporary build files (can delete)
└── label_printer.spec ← PyInstaller spec file
Running the Executable
On Your Computer
- Double-click
dist/LabelPrinter.exe - App starts immediately (first run takes ~5 seconds)
- Works like the Python version
Sharing with Others
- Copy
dist/LabelPrinter.exeto a folder - Create a shortcut to it on the desktop
- Share the folder or executable
- No Python installation needed on their computer!
Creating a Shortcut
- Right-click
LabelPrinter.exe - Send to → Desktop (create shortcut)
- Double-click the shortcut to run
Troubleshooting
"Failed to build the executable"
Solution 1: Check Python version
python --version # Should be 3.11+
Solution 2: Update PyInstaller
pip install --upgrade pyinstaller
Solution 3: Install missing dependencies
pip install -r requirements_windows.txt
pip install pyinstaller
"DLL load failed" when running exe
This usually means a library isn't bundled correctly.
Solution: Rebuild with verbose output
pyinstaller --debug=imports label_printer_gui.py
Executable is very large (200+ MB)
This is normal for Kivy applications. The size includes:
- Python runtime (~50 MB)
- Kivy framework (~30 MB)
- Dependencies (PIL, barcode, reportlab, etc.) (~20 MB)
- Your code (~1 KB)
You can reduce size slightly with:
--exclude-module=matplotlib
--exclude-module=numpy
--exclude-module=scipy
Slow to start (5-10 seconds)
Normal for Kivy apps. The first startup initializes:
- Python runtime
- Kivy graphics system
- Font rendering
- Window initialization
Subsequent runs are faster (~3 seconds).
Advanced Options
Add an Icon
- Create a 256x256 PNG icon:
app_icon.png - Convert to ICO: Use an online tool or ImageMagick
- Build with icon:
pyinstaller --icon=app_icon.ico label_printer_gui.py
Two-File Distribution
Instead of --onefile, use separate files for faster startup:
pyinstaller label_printer_gui.py
Creates dist/ folder with all files (faster to run, easier to debug).
Console Output
To see error messages, remove --windowed:
pyinstaller --onefile --name=LabelPrinter label_printer_gui.py
Build Options Reference
| Option | Purpose |
|---|---|
--onefile |
Single executable (recommended) |
--windowed |
No console window |
--icon=file.ico |
Custom icon |
--hidden-import=module |
Include module that's not imported directly |
--collect-all=module |
Include all module data |
--distpath=folder |
Output directory |
--name=AppName |
Executable name |
Final Steps
- Test the executable: Run
LabelPrinter.exeand test all features - Verify PDF backup: Check
pdf_backup/folder is created - Test printing: Print a label to ensure PDF output works
- Share: Distribute the
.exefile to users
Questions?
- Check the error message in the console
- Try rebuilding with
python build_exe.py - Ensure all dependencies are installed:
pip install -r requirements_windows.txt - Check that Python 3.11+ is installed:
python --version
Good luck! 🚀