Bundle Simple Python Application using PyInstaller

Bundle Simple Python Application using PyInstaller

How to build a self-contained python package (executable) using PyInstaller library

Introduction

PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules. PyInstaller is tested against Windows, MacOS X, and Linux. However, it is not a cross-compiler; to make a Windows app you run PyInstaller on Windows, and to make a Linux app you run it on Linux, etc.

Prerequisite

  • Operating system: Windows 10 or above (It can be used in other OS as well)
  • Python version: 3.7.6 or above (It works for python 2.7 versions as well)

Please download and install python 3 in your system. You can follow this video to install python 3. Then install PyInstaller from PyPI using command pip install pyinstaller.

Building executable

  • Lets say we have only one file main.py
    • Run command pyinstaller --onefile main.py
    • This will create two folders dist and build
    • You can find main.exe inside dist folder
    • This command will also create main.spec file in current directory (We will talk about this in next article)
  • Useful flags
    • -D,--onedir: one-folder bundle containing an executable
    • -F, --onefile: one-file bundled executable
    • -n NAME, --name NAME: Name for bundled app
    • --distpath DIR: Location of bundled app
  • Please follow this documentation for all the flags available for pyinstaller.

Reference

  1. PyInstaller official documentation