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:
- Import the
hy
mode - Import the
hy
script - 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.