How To Package Python Scripts Using Pyinstaller

This is a concise tutorial on packaging python script files ( .py files ) using pyinstaller.

1. Install Pyinstaller.

  1. Run the command pip install pyinstaller in the terminal window to install the python pyinstaller module.
    (MyPythonEnv) C:\Users\zhaosong>pip install pyinstaller
    Collecting pyinstaller
      Downloading pyinstaller-4.6-py3-none-win_amd64.whl (1.9 MB)
         |████████████████████████████████| 1.9 MB 39 kB/s
    Collecting pyinstaller-hooks-contrib>=2020.6
      Downloading pyinstaller_hooks_contrib-2021.3-py2.py3-none-any.whl (200 kB)
         |████████████████████████████████| 200 kB 25 kB/s
    Collecting pefile>=2017.8.1
      Downloading pefile-2021.9.3.tar.gz (72 kB)
         |████████████████████████████████| 72 kB 25 kB/s
    Collecting pywin32-ctypes>=0.2.0
      Downloading pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB)
    Requirement already satisfied: setuptools in c:\users\zhaosong\anaconda3\envs\mypythonenv\lib\site-packages (from pyinstaller) (58.0.4)
    Collecting altgraph
      Downloading altgraph-0.17.2-py2.py3-none-any.whl (21 kB)
    Collecting future
      Downloading future-0.18.2.tar.gz (829 kB)
         |████████████████████████████████| 829 kB 22 kB/s
    Building wheels for collected packages: pefile, future
      Building wheel for pefile ( ... done
      Created wheel for pefile: filename=pefile-2021.9.3-py3-none-any.whl size=68844 sha256=42ddeb63b48399239f9b7ffc7f8148b389307ee76427c60d602c88b481e0a7d2
      Stored in directory: c:\users\zhaosong\appdata\local\pip\cache\wheels\ee\a0\e1\ff2dafe5ae846f536acf0a4cc4a8b8d4ccb3044ab87f3ef174
      Building wheel for future ( ... done
      Created wheel for future: filename=future-0.18.2-py3-none-any.whl size=491070 sha256=486c247d31a3aaf5f7256955f8d4a063ca3051479be6f3830cc9bbce1c12f918
      Stored in directory: c:\users\zhaosong\appdata\local\pip\cache\wheels\8e\70\28\3d6ccd6e315f65f245da085482a2e1c7d14b90b30f239e2cf4
    Successfully built pefile future
    Installing collected packages: future, pywin32-ctypes, pyinstaller-hooks-contrib, pefile, altgraph, pyinstaller
    Successfully installed altgraph-0.17.2 future-0.18.2 pefile-2021.9.3 pyinstaller-4.6 pyinstaller-hooks-contrib-2021.3 pywin32-ctypes-0.2.0
  2. Run the command pip show pyinstaller to verify that it has been installed successfully.
    (MyPythonEnv) C:\Users\zhaosong>pip show pyinstaller
    Name: pyinstaller
    Version: 4.6
    Summary: PyInstaller bundles a Python application and all its dependencies into a single package.
    Author: Hartmut Goebel, Giovanni Bajo, David Vierra, David Cortesi, Martin Zibricky
    License: GPLv2-or-later with a special exception which allows to use PyInstaller to build and distribute non-free programs (including commercial ones)
    Location: c:\users\zhaosong\anaconda3\envs\mypythonenv\lib\site-packages
    Requires: pyinstaller-hooks-contrib, altgraph, pywin32-ctypes, pefile, setuptools
  3. If the installation fails, you can go to ~gohlke/pythonlibs/ to download the compiled WHL file.
  4. You can search the keyword pyinstaller on the above download page to find the PyInstaller‑3.6‑py2.py3‑none‑any.whl file.
  5. Download it and run the below command to install it.
    pip install PyInstaller‑3.6‑py2.py3‑none‑any.whl

2. General Packaging.

  1. To package the file, you can simply execute the command pyinstaller on the terminal. Note: the terminal needs to switch to the directory where file is saved.
  2. Common options and descriptions.
    1. -F: Only a single exe format file is generated after packaging.
    2. -D: The default option is to create a directory containing exe files and a large number of dependent files.
    3. -c: The default option is to use the console (a black window similar to dos window).
    4. -w: Do not use console.
    5. -p: Add a search path to find the corresponding library.
    6. -i: Change the icon of the generated program.

3. Special Packaging.

  1. We can edit the spec file to meet some special packaging requirements. So, what is a spec file?
  2. Its purpose is to tell pyinstaller how to package your .py file. When you use the command to automatically package .py files on the terminal, pyinstaller will automatically create a spec file first.
  3. In general, we do not need to edit the spec file ourselves, except as follows: 1) Resource files need to be packaged, 2) Add the run-time option to the executable file, or include some run-time libraries that pyinstaller does not know.
  4. To generate a spec file for, just execute the following command on the terminal. The command options are the same as pyinstaller.
  5. Here is a simple example of a spec file.
    # -*- mode: python -*-
    block_cipher = None
    a = Analysis([''],
    pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
    exe = EXE(pyz,
              console=True )
    coll = COLLECT( exe,
  6. Analysis: Used to define Python source files, including search path, source file name, etc.
    1) scripts:Source files defined in the Analysis.
    2) pure:Python module.
    3) binaries:Dynamic library.
    4) datas:Data files, including pictures, fonts, etc.
    5) zipfiles:Dependent files in zip format, they are generally library files in egg format.
  7. PYZ: Compress and package Python files, including all dependencies required for program operation. The input is generally Analysis.pure.
  8. EXE: Package and generate EXE files according to the above two items. The EXE subtask includes all five output items of Analysis, as well as some configuration files and dynamic libraries required for program operation.
  9. The configuration file and dynamic library are configured in TOC format (name, path, typecode), below is an example.
    exe = EXE(pyz, a.scripts, exclude_binaries=True, name='abc', debug=False, strip=False, upx=True, console=True )
  10. The typecode includes the below values.
    1. EXTENSION:python extension library.
    2. PYSOURCE:python script.
    3. PYMODULE;
    4. PYZ;
    5. PKG;
    6. BINARY:Dynamic library.
    7. DATA:Data files.
    8. OPTION
  11. COLLECT: It is used to build the final generation directory. You can copy the results generated by other subtasks and copy them to the specified directory to form the final packaging results. This configuration item is not a must-have item, you can use the default COLLECT value by not configuring it.
  12. After editing the abc.spec file, execute the following command on the terminal to generate the required app (.exe) file.
    pyinstaller abc.spec
    Command options include:

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.