PyInstaller bundles a Python application and all its dependencies into a single package.
npx @tessl/cli install tessl/pypi-pyinstaller@6.15.0PyInstaller is a mature Python application bundling tool that converts Python scripts and their dependencies into standalone executable packages. It analyzes Python code to discover all required modules and libraries, then collects copies of those files along with the Python interpreter into a single folder or executable file.
pip install pyinstallerimport PyInstallerFor programmatic usage:
from PyInstaller import run
from PyInstaller.building.build_main import Analysis
from PyInstaller.building.api import PYZ, EXE, COLLECTFor hook development:
from PyInstaller.utils.hooks import collect_submodules, collect_data_files# Create a standalone executable from a Python script
pyinstaller myscript.py
# Create a one-file executable
pyinstaller --onefile myscript.py
# Create a windowed application (no console)
pyinstaller --windowed myapp.py
# Add additional data files
pyinstaller --add-data "data/*:data" myscript.py
# Exclude modules to reduce size
pyinstaller --exclude-module matplotlib myscript.pyfrom PyInstaller import run
# Run PyInstaller programmatically
run(['myscript.py', '--onefile', '--name=MyApp'])# myapp.spec
from PyInstaller.building.build_main import Analysis
from PyInstaller.building.api import PYZ, EXE
a = Analysis(
['myapp.py'],
pathex=[],
binaries=[],
datas=[('data/', 'data/')],
hiddenimports=['hidden_module'],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None,
noarchive=False
)
pyz = PYZ(a.pure, a.zipped_data, cipher=None)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='MyApp',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True
)PyInstaller uses a multi-stage build process:
The bootloader is a small C program that extracts and executes the bundled Python application at runtime, providing cross-platform compatibility without requiring Python installation on target systems.
Command-line interface for creating standalone executables from Python applications with extensive configuration options for output format, optimization, and bundling behavior.
def run(pyi_args=None, pyi_config=None):
"""
Main entry point for running PyInstaller programmatically.
Args:
pyi_args (list): Command-line arguments (defaults to sys.argv[1:])
pyi_config (dict): Configuration dictionary for multiple builds
"""Core classes used in .spec files to define the build process, including dependency analysis, archive creation, and executable generation.
class Analysis:
"""Performs dependency analysis of Python scripts."""
def __init__(self, scripts, pathex=[], binaries=[], datas=[],
hiddenimports=[], hookspath=[], runtime_hooks=[],
excludes=[], **kwargs): ...
class PYZ:
"""Creates zlib-compressed archive of compiled Python modules."""
def __init__(self, *tocs, name=None): ...
class EXE:
"""Creates final executable from PKG and bootloader."""
def __init__(self, *args, name='', console=True, debug=False, **kwargs): ...
class COLLECT:
"""Collects files into directory for onedir distribution."""
def __init__(self, *args, name='', strip_binaries=False, **kwargs): ...Functions for writing PyInstaller hooks to handle special cases in dependency collection, including module discovery, data file collection, and metadata handling.
def collect_submodules(package, filter=None, on_error="warn once"):
"""Recursively collect all submodules of a package."""
def collect_data_files(package, include_py_files=False, subdir=None,
excludes=None, includes=None):
"""Collect data files from package installation."""
def collect_all(package_name, include_py_files=True, **kwargs):
"""Collect all components for a package."""
def can_import_module(module_name):
"""Test if module can be imported."""Command-line tools for archive inspection, dependency analysis, version handling, and spec file generation.
# Console scripts available as commands:
# pyi-archive_viewer - Interactive archive viewer
# pyi-bindepend - Binary dependency analysis
# pyi-grab_version - Windows version extraction
# pyi-makespec - Spec file generation
# pyi-set_version - Windows version settingWindows and macOS specific functionality including icon handling, version resources, manifest embedding, code signing, and app bundle creation.
# Windows utilities (PyInstaller.utils.win32)
# - icon: Icon file handling
# - versioninfo: Version resource utilities
# - winmanifest: Manifest handling
# macOS utilities (PyInstaller.building.osx)
class BUNDLE:
"""Creates macOS application bundle (.app)."""
def __init__(self, *args, name='', icon=None, bundle_identifier=None, **kwargs): ...# Version and path constants
__version__: str # PyInstaller version
HOMEPATH: str # PyInstaller home directory
PLATFORM: str # Platform identifier
DEFAULT_DISTPATH: str # Default output directory (./dist)
DEFAULT_SPECPATH: str # Default spec file directory (.)
DEFAULT_WORKPATH: str # Default working directory (./build)class ExecCommandFailed(SystemExit):
"""Command execution failure."""
class HookError(Exception):
"""Base class for hook-related errors."""
class PythonLibraryNotFoundError(IOError):
"""Python shared library not found."""
class RemovedCipherFeatureError(SystemExit):
"""Deprecated cipher feature usage."""