How to Package a Python GUI Application with Dependencies Using PyInstaller
This guide explains how to structure a Python project, list required libraries in requirements.txt, write a main script that installs dependencies at runtime, and use PyInstaller to bundle the application and its assets into a single executable that automatically installs needed packages.
Requirement: The executable should automatically read dependencies from a provided Python package and download them to a specified directory during installation, allowing the user to select the package and target directory.
Step 1: Prepare your project structure Ensure your project follows this layout:
project/
│
├── main.py
├── requirements.txt
└── gui/
└── assets/
└── (your GUI assets)The requirements.txt file should list all required libraries, for example:
numpy
pandas
matplotlibStep 2: Write main.py This script reads requirements.txt , installs the dependencies into a dependencies folder, and then launches a simple Tkinter GUI.
import tkinter as tk
import subprocess
import sys
import os
def install_dependencies():
# Get the directory of the current script
script_dir = os.path.dirname(os.path.abspath(__file__))
# Specify the target directory for installed packages
target_dir = os.path.join(script_dir, 'dependencies')
# Create the directory if it does not exist
if not os.path.exists(target_dir):
os.makedirs(target_dir)
# Path to requirements.txt
requirements_file = os.path.join(script_dir, 'requirements.txt')
# Install packages to the target directory
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--target', target_dir, '-r', requirements_file])
def run_gui():
root = tk.Tk()
root.title("My GUI Application")
label = tk.Label(root, text="Hello, World!")
label.pack()
root.mainloop()
if __name__ == '__main__':
install_dependencies()
run_gui()Step 3: Package the application with PyInstaller Open a command prompt, navigate to the project directory, and run:
pyinstaller --onefile --add-data "requirements.txt;." --add-data "gui/assets:gui/assets" main.pyThis command does the following:
--onefile: creates a single executable file.
--add-data "requirements.txt;.": includes the
requirements.txt
file in the bundled resources.
--add-data "gui/assets:gui/assets": includes the GUI assets directory (optional, only needed if you have assets).Step 4: Test the executable After packaging, you will find the .exe file in the dist/ folder. Running it will automatically detect and install the required libraries, then launch the GUI.
Notes
Make sure requirements.txt contains all necessary libraries.
The target machine must allow pip installations; ensure Python is available.
If any dependencies contain binary components, you may need to build them separately for each target OS or find cross‑platform alternatives.
Following these steps creates a self‑contained Python GUI application that simplifies deployment and improves independence.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.