PyInstaller
PyInstaller is a versatile tool that bundles a Python application and all its dependencies into a single package, often a standalone executable. This allows distribution of Python applications to users without requiring them to install a Python interpreter or any modules. The current version is 6.19.0, and the project typically releases minor versions every 2-3 months and major versions annually.
Warnings
- breaking Python Version Compatibility: PyInstaller regularly updates its supported Python versions. Upgrading PyInstaller might require upgrading your Python environment if you're on an older, no longer supported version.
- deprecated The `--add-data` and `--add-binary` command-line options syntax for specifying source and destination paths was modified starting with PyInstaller 5.0 to improve cross-platform consistency. The old syntax (e.g., `SRC;DEST` for Windows or `SRC:DEST` for Unix-like systems) is deprecated and will emit warnings.
- gotcha Missing runtime dependencies or 'hidden imports': Complex libraries often use dynamic imports or C extensions that PyInstaller might not automatically detect. This leads to runtime errors in the bundled application.
- gotcha Antivirus software false positives: Executables bundled with `--onefile` (a single executable file) can sometimes be flagged by antivirus software as suspicious, even if they are legitimate, due to their compressed nature and unpack-on-run behavior.
Install
-
pip install pyinstaller
Quickstart
import os
# Create a dummy Python script for demonstration
with open('my_app.py', 'w') as f:
f.write("""
import sys
import os
def main():
print("Hello from my bundled app!")
print(f"Running from: {os.path.dirname(sys.executable)}")
if __name__ == '__main__':
main()
""")
# Run PyInstaller via command line (most common use case)
# For programmatic use, you'd typically call PyInstaller.__main__.run(['my_app.py'])
# This is a shell command, not Python code to be executed directly in this context.
# You would run 'pyinstaller my_app.py' in your terminal.