Gangmax Blog

Pyinstaller Notes

Recently I’m using “pyinstaller” and here are some notes from my recent experience.

How to include Python files loaded dynamically?

From here and here.

In my code I use “importlib.import_module(…)” to load module dynamically. After packaged with “pyinstaller”. Running the program gets error that the target module cannot be found. The reason is, “pyinstaller” will parse the Python code and only include the modules explicitly imported.

To fix the issue, I can either:

  1. Add explicit “import” statement in Python code(it’s OK not to use the explicitly imported module since the dynamically importing will work anyway);

  2. Add the “–hidden-import” argument(s) when executing “pyinstaller”, such as:

pyinstaller --hidden-import 'tap.commands' --hidden-import ''

I choose option 1 since adding the explicit “import” statements is much clearer than updating the “pyinstaller” command.

How to package data(resource) files?

From here and here.

In my code, some data files locates at a project package which are loaded on runtime. The code works file until it’s running within the executable file compiled by “pyinstaller”. The reason is that such data files are not included when “pyinstaller” does the packaging. To fix this issue, 2 steps are required:

  1. Add the “–add-data” argument when executing “pyinstaller”, such as:
pyinstaller --add-data "tap/commands/*.template:tap/commands"
  1. Update project code by using “pkgutil” to read the data file instead of reading the target file directly:
import pkgutil
template_bin = pkgutil.get_data('tap', 'commands/id.template')
template = template_bin.decode('UTF-8', 'ignore')


Packaging everything into one executable file makes running a Python program much easier, but it breaks some fundamental assumptions how a Python program runs, which brings some extra cost to make it work.