Skip to content

Instantly share code, notes, and snippets.

@jloow
Created March 15, 2021 10:16
Show Gist options
  • Save jloow/0d255e8d691882b0763839e436b5d7ca to your computer and use it in GitHub Desktop.
Save jloow/0d255e8d691882b0763839e436b5d7ca to your computer and use it in GitHub Desktop.
Some notes on getting hy to work with pyinstaller

Using hy causes some problems with regards to exporting the project using pyinstaller.

First, it is not possible to export using a hy file directly. Instead, one has to write a simple wrapper. The wrapper needs to do three things:

  1. Import the hy mode
  2. Import the hy script
  3. Call a main or init function of the hy script

An example wrapper would look like:

import hy
import the_hy_script as s

s.main()  

The hy script is not automatically included in the export and thus has to be specified manually using the following flag: --add-data "the_hy_script.hy;." . In the "a;b" syntax, a refers to the location of the script and b its location in the export folder. Note also that the hy script cannot contain hyphens as this is not valid syntax for import statements in python.

Then, when exporting, some further problems surface, namely those detailed here. That post also provides a solution, particularly as exemplified by this GitHub repo. Essentally, one has to include a hook file (hook-hy.py) with the following content:

# -*- mode: python -*-

from PyInstaller.utils.hooks import collect_data_files

datas = collect_data_files('hy', include_py_files=True)

The location of the hook file then needs to be indicated with the appropriate flag: --additional-hooks-dir ".".

Furthermore, as the pyinstaller is not made for hy, pyinstaller does not automatically detect imported modules in the hy script. These therefore have to specified manually using the following flag: --hidden-import "ModuleName" (note that the module name is case sensitive).

Finally, for some reason, if exporting to a single file (using the -F flag) Windows will flag the file as a potential threat and remove it (this does not seem happen if one exports only py files); this is solved by simply using the -D flag (which is the default).

Thus, the final export command would look somewhat like this:

pyinstaller wrapper.py --add-data "the_hy_script.hy;." --additional-hooks-dir "." --hidden-import "ModuleName" -y  

The -y flag is to overwrite any existing export folders with the same name as the exported project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment