ahk icon indicating copy to clipboard operation
ahk copied to clipboard

Docs: How to compile to a standalone single file with Nuitka

Open laundmo opened this issue 4 years ago • 7 comments

I have recently attempted, and after some effort managed, to compile a python ahk script to a standalone single file exe using nuitka.

Heres the process:

  • Install nuitka
  • Copy your AutoHotkey.exe into the same folder as your script
  • Use this code block to create your AHK instance, since when compiling to a single file with nuitka the path needs to be specified relative to __file__
    from ahk import AHK
    from pathlib import Path
    
    AHK_EXE = Path(__file__).parent / "AutoHotkey.exe"
    ahk = AHK(executable_path=str(AHK_EXE))
    
  • compile to a single file nuitka exe with the following command
    py -m nuitka --include-data-file=AutoHotkey.exe=./AutoHotkey.exe --include-package-data=ahk --onefile --standalone script.py
    
    --include-data-file will include the ahk exe in the single file app, and --include-package-data is required to include all the templates that this library uses.

I thought this might be interesting to document, since one of the best features of auto hotkey is IMO that it compiles to a standalone exe that i can share. I wasn't sure whether this is documentation that is wanted, or in what form/where, hence the issue and not a PR.

laundmo avatar Dec 06 '21 02:12 laundmo

Thanks for this! I agree ability to compile to standalone executables is a key feature that users are looking for.

After we get to a 1.0 release I'll work to document (and add automated testing) how to compile to a standalone exe for at least one tool (probably pyinstaller at first, then maybe nuitka shortly thereafter).

Before 1.0, the implementation details around the AHK code template files is likely to change somewhat and may impact instructions for bundling ahk, so my thought was to avoid writing too much documentation around this just yet :-)

Until then, this issue can stand as a good reference points for users trying to use nuitka with AHK. #46 has some discussion around pyinstaller.

spyoungtech avatar Dec 08 '21 07:12 spyoungtech

hm, i don't really know whether template changes could cause any issues, since Nuitka auto-discovers the templates based on package name. so unless they are moved outside the libraries install directory, it should continue working.

Another thing to consider for the 1.0 release is asking the Nuitka devs about adding ahk include files to Nuitka directly, they already do this for common libraries like NumPy and Tkinter.

laundmo avatar Dec 08 '21 09:12 laundmo

Another thing to consider for the 1.0 release is asking the Nuitka devs about adding ahk include files to Nuitka directly, they already do this for common libraries like NumPy and Tkinter.

Do you mean for the AutoHotkey.exe file? I'm not familiar with Nuitka, but this library is certainly not as popular as numpy/tk and I'm not sure if we could reasonably warrant the attention of the Nuitka developers :)

There is also the ahk-binary package (can be installed as an 'extra': pip install "ahk[binary]"), which serves the purpose of providing that file when installed (to a location where it should be on PATH, if pip is also on PATH).

I'm wondering if that were used, maybe users of Nuitka can add something like --include-package-data ahk-binary and perhaps that would do the trick and avoid the step to copy AutoHotkey.exe.
It might also remove the need to provide executable_path -- ahk-binary normally adds AutoHotkey.exe to the <python-install>\Scripts directory, making it available on PATH, so ahk finds it automatically with shutil.which. But I'm actually not sure if this works in practice with an app bundled with Nuitka.

spyoungtech avatar Dec 10 '21 19:12 spyoungtech

Do you mean for the AutoHotkey.exe file?

i was more thinking about the template files, the ahk.exe is a bit much to ask, so compiling with just the templates would mean scripts would run on systems where ahk is installed.

There is also the ahk-binary package

i wasn't aware of this, gonna make a note to test this once i have some time.

But I'm actually not sure if this works in practice with an app bundled with Nuitka.

yeah, i would think not, since nuitka unpacks dependencies to the temp folder, so this library likely wont be able to find the file.

laundmo avatar Dec 11 '21 14:12 laundmo

v1 now includes better support for freezing. Missing template issues are now resolved by falling back to string constants with the template content. So that should no longer be a concern for compiling.

So, the only thing you need to ensure is that either (1) the target machine has AHK available (on PATH or in a standard location) or (2) that your frozen bundle includes the autohotkey executable and you set executable_path accordingly.

spyoungtech avatar May 02 '23 09:05 spyoungtech