Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
About Article
Analyze Data
Archive
Best Practices
Better Outputs
Blog
Code Optimization
Code Quality
Command Line
Daily tips
Dashboard
Data Analysis & Manipulation
Data Engineer
Data Visualization
DataFrame
Delta Lake
DevOps
DuckDB
Environment Management
Feature Engineer
Git
Jupyter Notebook
LLM
LLM
Machine Learning
Machine Learning
Machine Learning & AI
Manage Data
MLOps
Natural Language Processing
NumPy
Pandas
Polars
PySpark
Python Tips
Python Utilities
Python Utilities
Scrape Data
SQL
Testing
Time Series
Tools
Visualization
Visualization & Reporting
Workflow & Automation
Workflow Automation

PyInstaller: Bundle a Python Application Into a Single Executable

Table of Contents

PyInstaller: Bundle a Python Application Into a Single Executable

Motivation

Data scientists and analysts often need to share their Python applications with non-technical users who don’t have Python installed. Distributing Python scripts that require installing Python, managing dependencies, and setting up environments creates barriers for end-users.

Example:

# Instructions for end users:
# 1. Install Python 3.8+
# 2. Create virtual environment
# 3. Install requirements:
pip install pandas numpy scikit-learn matplotlib

# 4. Run the script:
python simple_df.py

# This process is complex for non-technical users

Introduction to PyInstaller

PyInstaller is a tool that bundles a Python application and all its dependencies into a single package or executable file. This allows end-users to run the application without installing Python or any additional dependencies.

Installation:

pip install pyinstaller

Creating Standalone Executables

PyInstaller solves the distribution problem by:

  • Analyzing your code to identify all required dependencies
  • Collecting all necessary files including the Python interpreter
  • Creating a single executable or folder that can run on the target platform

Here’s an example of using PyInstaller:

Create a simple script that prints a pandas DataFrame:

# simple_df.py
import pandas as pd

df = pd.DataFrame({
    'Name': ['John', 'Anna', 'Peter'],
    'Age': [28, 24, 35],
    'City': ['New York', 'Paris', 'London']
}
print("\nEmployee Information:")
print("===================")
print(df)

Create the executable:

# Basic usage
pyinstaller simple_df.py

# Create single file executable
pyinstaller --onefile simple_df.py

The executable will be created in the dist directory. Users can run it without installing Python:

# Windows
dist\simple_df.exe

# Linux/Mac
./dist/simple_df

Output:

Employee Information:
===================
    Name  Age     City
0   John   28  New York
1   Anna   24    Paris
2  Peter   35   London

Conclusion

PyInstaller makes it easy to distribute Python applications to non-technical users by creating standalone executables. This is particularly useful for data scientists who want to share their analysis tools with stakeholders who don’t have Python expertise.

Link to PyInstaller

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran