google / grumpy

Grumpy is a Python to Go source code transcompiler and runtime.
Apache License 2.0
10.56k stars 652 forks source link

How to import and call function(written in python and transferred into go by grumpc) in go? #407

Open always-less opened 5 years ago

always-less commented 5 years ago

As titled, the newest grumpy.ImportModule function has only 2 params while the comment has not been updated.

And ImportModule(f, "a.b") causes the initialization and entry into sys.modules of Grumpy module a and then Grumpy module a.b instead of b. Is this a bug?

davejohnston commented 5 years ago

I could do with some help on this too. Following the steps in the README.md https://github.com/google/grumpy I've done the following:

echo 'def hello(): print "hello, world"' > $GOPATH/src/__python__/hello.py

grumpc -modname=hello $GOPATH/src/__python__/hello.py > \
    $GOPATH/src/__python__/hello/module.go

I can now use grumprun to import the module in python echo 'from hello import hello; hello()' | grumprun

This produces the output

hello, world

What I can't do is figure out how to import the module hello in a golang main and run it. I've tried the following:

func main() {
    grumpy.ImportModule(grumpy.NewRootFrame(), "traceback")
    os.Exit(grumpy.RunMain(hello.Code))
}

This runs, but produces no output. I'm guessing I need to call the hello() method that is generated in the init() block of module.go, but I can't see how that should be done ?