Embedding Python 3.9 in autohotkey by using Windows COM

[UPDATED 2020-08-03: using python 3.9]

Without a doubt, Autohotkey is the best program for hotkeys under Windows. It contains an easy script language for binding hotkeys and shortcuts to any possible key combination.

Autokotkey allows to write emails or code in any editor faster than ever before. Simple tasks can be automated and much more. And the best part: it is open source.

On the other hand, python is a powerful script language with dozens of extra packages. From simple file task to complex machine learning everything is available free of charge.

Of course, people tried to combine these two tools before. The most common way is to use ctypes for binding the python DLL into autohotkey.

But there is a much more convenient and more flexible solution: the windows COM interface This allows you to have a complete python interpreter embedded in autohotkey

With only 1 line of code Python is ready to be used in autohotkey
The following example shows a message box with the installed python version

; File name: python_test.ahk

; Create python interpreter com object
py := ComObjCreate("Python.Interpreter")

; Get version and display a message box
py.Exec("import sys")
r :=  py.Eval("sys.version")
msgbox % "Python Version " + r

Here is a simple step by step guide on how to setup (for x32 and x64)

1.) Install pywin32 by using pip

pip3 install pywin32

2.) Register the python COM object

"C:\Program Files\Python39\Lib\site-packages\win32com\servers\interp.py"

You should see this output:

Registering COM server...
Registered: Python.Interpreter
3.) Set the windows path to your python COM DLL

Path to add: C:\Program Files\Python39\Lib\site-packages\pywin32_system32

There are two ways of doing this:

  1. opening the system properties (WinKey + Pause)
  2. selecting the “Advanced” tab
  3. click the “Environment Variables” button
  4. then adding or selecting the PATH variable

or (more convenient) in a terminal window:

rundll32 sysdm.cpl,EditEnvironmentVariables

4.) Congratulation you are done

Ressources:

[1] Autohotkey:
https://en.wikipedia.org/wiki/AutoHotkey
[2] pywin32
https://pypi.org/project/pywin32/

 

4 thoughts on “Embedding Python 3.9 in autohotkey by using Windows COM

  1. Anonymous Reply

    Followed the instructions. Can’t edit the path. What is wrong

Leave a Reply

Your email address will not be published. Required fields are marked *