go-python / gpython

gpython is a python interpreter written in go "batteries not included"
BSD 3-Clause "New" or "Revised" License
869 stars 95 forks source link

Embed into another program #138

Closed alexec closed 2 years ago

alexec commented 3 years ago

Is it possible to embed this? Like Otto (https://github.com/robertkrimen/otto).

sbinet commented 3 years ago

as can be seen in https://github.com/go-python/gpython/blob/master/main.go#L120, embedding a gpython vm is like:

res, err := vm.Run(module.Globals, module.Globals, code, nil)

ie:

is that too low level an API?

alexec commented 3 years ago

I actually figured it out:

https://github.com/argoproj/argo-workflows/blob/master/util/python/python.go

I wrote code to convert Go types into Python types. Is there another way?

miathedev commented 2 years ago

My question is similar. I would like to execute methods of a Python script from my Golang application. Is that possible in a easy way? I need something more high-level fashioned.

sbinet commented 2 years ago

I'll try to provide a simple example. but this will have to wait for https://github.com/go-python/gpython/pull/144 to be merged (as the API is changing a bit to allow for multiple VM-contexts to run concurrently)

drew-512 commented 2 years ago

In the coming days I'll PR the main README and add updated docs and snippets for newcomers to more easily get up and running. The changes coming offer a couple new high-level calls that make life very nice to use and embed gpython.

sbinet commented 2 years ago

fixed w/ https://github.com/go-python/gpython/pull/159