Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create golang bindings for a python module

I want to write golang bindings for an existing (third party) Python module. The purpose is that I want to use the API that the Python module provides in Golang.

I already found golang bindings for Python's C API (go-python3 for py3 and go-python for py2), but I still haven't figured out how to translate a relatively complex Python module into Golang (i.e. how to deal with type safety in go for unsafe inputs and returns in python, etc).

What would be a good approach? Are there any pre-existing tools in that space? Are there any good examples for Golang bindings for Python code? (I couldn't find many tbh).

like image 466
Chris Avatar asked Aug 20 '20 17:08

Chris


People also ask

Can you call Python from go?

But we still can call Python in memory from Go (as a shared library), which is relatively fast. So compared to the shared memory solution from the Ardan Labs blogpost I'd argue it's a tradeoff for more approachability at the cost of reduced efficiency.


1 Answers

I want to use the API that the Python module provides in Golang.

Calling Python from Go is detailed recently in "Python and Go : Part I - gRPC" by Miki Tebeka.
You can see an example in ardanlabs/python-go/grpc

But, as shown in their next two articles, you can also:

  • compiled Go code to a shared library and used it from the Python interactive shell.
  • use a Python module that hides the low level details of working with a shared library and then package this code as a Python package.

https://www.ardanlabs.com/images/goinggo/124_figure1.png

Full example: ardanlabs/python-go/pyext.

like image 89
VonC Avatar answered Sep 29 '22 22:09

VonC