14

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

3
  • 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. Commented Sep 2, 2020 at 8:49
  • maybe let me rephrase: Is there any good example for how to use go-python3 or go-python? Commented Sep 2, 2020 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", 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)? Commented Sep 2, 2020 at 11:03

1 Answer 1

13
+100

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 THank you. I am surprised this issue reported in 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.
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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.