ahk icon indicating copy to clipboard operation
ahk copied to clipboard

How to execute frozen script as exe with PyInstaller? TemplateNotFound

Open Timurinyo opened this issue 6 years ago • 5 comments

Error while trying to execute script freezed to exe with pyinstaller: Traceback (most recent call last): File "autoLogin.py", line 4, in File "site-packages\ahk\mouse.py", line 88, in mouse_move File "site-packages\ahk\mouse.py", line 84, in _mouse_move File "site-packages\ahk\script.py", line 49, in render_template File "site-packages\jinja2\environment.py", line 830, in get_template File "site-packages\jinja2\environment.py", line 804, in _load_template File "site-packages\jinja2\loaders.py", line 113, in load File "site-packages\jinja2\loaders.py", line 187, in get_source jinja2.exceptions.TemplateNotFound: mouse/mouse_move.ahk [12584] Failed to execute script autoLogin

Timurinyo avatar Jun 06 '19 15:06 Timurinyo

Hmm. It may be tricky to freeze ahk in its current state. Right now, I build the path to templates folder using __file__ attribute of the script module. When running in a PyInstaller bundle, __file__ is not available at runtime.

Additionally, it would likely be necessary to add the template files to your bundle. See the pyinstaller docs on this.

A possible solution to this would be switching to the Jinja2 package loader, rather than using filesystem loader, which is something I'd like to do for other reasons, but have not quite gotten around to it yet.

spyoungtech avatar Jun 06 '19 18:06 spyoungtech

Currently made it work by using "pyinstaller --onedir script.py" and then adding "ahk" folder with "templates" folder and also adding "AutoHotkey" folder with "AutoHotkey.exe" to "dist". I specified ahk executable_path as "ahk = AHK(executable_path='.\AutoHotkey\AutoHotkey.exe')"

Timurinyo avatar Jun 07 '19 14:06 Timurinyo

Hello Timurinyo, I am trying to also compiled a .py script that uses this library and I added the AutoHotKey folder to the project directory but I'm not sure of the templates, when you say 'adding "ahk" folder with "templates", what templates are you referring to specifically and where do I get them? Thank you

gusibis avatar Mar 02 '20 20:03 gusibis

Hello gusibis. ahk/templates is lying inside this repo.

Timurinyo avatar Mar 03 '20 08:03 Timurinyo

I had this exact issue a couple of days ago. The way I solved it was just by adding "--add-data "C:\Program Files\Python37\Lib\site-packages\ahk";ahk"

ItsIgnacioPortal avatar Nov 03 '20 16:11 ItsIgnacioPortal

v1 introduces better compatibility for freezing. There is also a fallback to string constants if templates aren't packaged for any reason.

spyoungtech avatar May 02 '23 05:05 spyoungtech