Embedding Python in AHK - revisited.

Post your working scripts, libraries and tools for AHK v1.1 and older
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Embedding Python in AHK - revisited.

Post by burque505 » 27 Aug 2021, 16:45

@bourdin07 posted this sometime back about embedding Python in AHK via COM. I found a link to that post in this blog post, which I was delighted to find. Here is a script that works for me. I have pythoncom37.dll in various places on my system, so I'm not sure if I registered it for COM or some program I installed (Kivy or Visual Studio, for example) registered it. As I don't remember doing it (which means nothing unfortunately) I think "Kivy did it."

Apparently for bourdin07 the script at [Lib\]site-packages\win32com\servers\interp.py didn't register the COM dll for him. As it was already registered for me when I tried it, the only thing I can think of to do if that script doesn't work is to use regsvr32 on the pythoncom[version_number].dll, or perhaps install kivy to see if that works.

This code works for me.

Code: Select all

py := ComObjCreate("Python.Interpreter")

;si(py)

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

py.Exec("import win32api")
r :=  py.Eval("win32api.GetComputerName()")
msgbox % r

py := ""

ExitApp

bourdin07
Posts: 36
Joined: 23 Jun 2019, 12:57

Re: Embedding Python in AHK - revisited.

Post by bourdin07 » 04 Sep 2021, 04:28

Thank you @burque505
I will try. I need to reinstall python to test :)

tpitera
Posts: 31
Joined: 27 Oct 2020, 15:56

Re: Embedding Python in AHK - revisited.

Post by tpitera » 15 Sep 2021, 20:28

@burque505 unfortunately doesnt work for me
If you find another way to do it, I would love to know what it is

burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Embedding Python in AHK - revisited.

Post by burque505 » 16 Sep 2021, 08:47

@tpitera, if you got an error thrown by 'ComObjCreate("Python.Interpreter")', then python**.dll isn't registered. If python throws some other error, you may not have installed pywin32. 'pip install pywin32'
Please try the steps in the link in the OP.
If none of those things work, maybe you can share some error messages.
Regards,
burque505

iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Embedding Python in AHK - revisited.

Post by iseahound » 25 Sep 2021, 17:07

Very useful. Python Libraries are much richer. Thank you!

burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Embedding Python in AHK - revisited.

Post by burque505 » 25 Sep 2021, 17:47

:thumbup: Hope it's of some use to you!
By the way, I just tried the installation process from the link on a Win10 machine, Python 3.7.8, it worked fine.
Regards,
burque505

20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: Embedding Python in AHK - revisited.

Post by 20170201225639 » 25 Sep 2021, 23:34

The example worked for me. Thanks!

Relatedly, there's also the method described here (though the use cases are probably somewhat different), which also works for me (I'm using it to call a python function to do some numerical computations, then return the value to AHK. It's also extremely fast, I regularly make 8000+ calls to the Python function and it always take less than 1 sec in total. If you follow this method, don't forget the step (described therein) where you need to register the com server.

https://stackoverflow.com/questions/65780086/how-to-program-hotstrings-in-python-like-in-autohotkey/65783573#65783573

burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Embedding Python in AHK - revisited.

Post by burque505 » 26 Sep 2021, 14:57

@20170201225639, thank you very much for that link. Maybe the code below will interest you. After visiting this link on SO that in turn links to this XeroByte thread I did a little experimenting.

1) The XeroByte approach uses DLLCall() instead of loading the COM object, and this seems to me to be quite a bit faster. But the hard-coding of the path for the Python dll seemed a little tedious. I have simplified it a bit, but not entirely. Once PythonXX.dll is registered, all you need is the name of the DLL and not the full path.
2) The problem referenced by the OP in the first thread above (running two Python scripts from the same AHK script) is one I haven't been able to fix yet with the Python COM object, but I've worked around it as shown below.
3) It's not going to work as written unless you register the COM server as indicated above.

This code uses 'pandas' in the first script var to read the table on the page at 'https://www.w3schools.com/html/html_tables.asp' (boy, does that page ever get abused), print it (how it outputs to console depends on how you approach it - in Scite4AHK the table will appear at the bottom once both scripts run), and write it to a .csv file (with a minor change you can write it straight to Excel), and then show a WinAPI message box from the second python script var.

I'm hoping this approach will have some use in RPA, web scraping and data science using AHK.

I used a couple of functions to make my life easier. This code is very, very far from perfect and I have a long way to go, but I'm encouraged nonetheless. (It's not going to be very portable until I get the Python version back in there automatically, which I obviously have not done yet. But maybe I can adapt XeroByte's code for snagging the version to this approach, doesn't seem like it'll be very difficult.)
Spoiler
Substitute in

Code: Select all

dfs[0].to_excel(r'C:\work\python\futterkiste.xlsx', index=False, header=False)
for

Code: Select all

dfs[0].to_csv(r'C:\work\python\futterkiste.csv', index=False, header=False)
if you want to write straight to .xlsx.

I'm very curious about something, and all help is greatly appreciated. Using NirSoft's dllexp.exe I can see the huge number of functions in Python37.dll. But the article I linked to in the first post only mentioned 'Py.Exec' and 'Py.Eval', neither of which appears in that form exactly in the 'dllexp.exe' list of functions exported. Does anyone happen to have a list of the functions that the COM server exposes when PythonXX.dll is called with 'ComObjCreate()'? Thanks in advance!

Output in Scite4AHK:
futterkiste.PNG
futterkiste.PNG (6.87 KiB) Viewed 1538 times

Regards,
burque505

Post Reply

Return to “Scripts and Functions (v1)”