13

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).

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Chris
  • 567
  • 6
  • 24
  • I think the question is too broad - it would be better to present a small python functionality you would like to call from go. To expect that somebody would explain all possible problems with all possible types of input is just a little bit over the top. – ead Sep 02 '20 at 08:49
  • maybe let me rephrase: Is there any good example for how to use go-python3 or go-python? – Chris Sep 02 '20 at 10:26
  • The rephrased question would be off-topic here, as "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow", https://stackoverflow.com/help/on-topic. go-python3 has some examples in the github-repository, why aren't they sufficient for your usecase (what ever that may be)? – ead Sep 02 '20 at 11:03

1 Answers1

12

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Your embedded PNG could really do with using a lighter grey (vs black) for the text.. or not using transparency. In dark mode where the background is `#2D2D2D` it's rather hard to read (classic light/dark UI conflict, just a suggestion) – Rudu Aug 29 '20 at 00:40
  • 2
    @Rudu THank you. I am surprised this issue reported in https://meta.stackoverflow.com/a/396071/6309 is yet to be solved. In the meantime, all I have to do is save the png in jpg, which loses the transparent effect. I have edited the answer and re-uploaded the picture, now readable in dark mode. – VonC Aug 29 '20 at 05:23
  • thanks for the link to this series of blog posts. It was certainly an interesting read, so thank you for it! But: Unfortunately it doesn't answer the question. I'm looking for a way to create golang bindings for a Python module. That means I want to create a golang program that dynamically links to the python DLL (i.e. with the mentioned libraries). I'm looking for advice or a good example how to write Go code that wraps/binds to Python code. (The first article covers grpc calls from a golang to a python process. The second describes writing a Python module in Go - both different topics). – Chris Aug 31 '20 at 19:25
  • @Chris I agree, this does not cover fully your question, but since it still allows a Go program to call a Python one, I figured it could be of interest for other readers. – VonC Aug 31 '20 at 20:08