sbinet / go-python

naive go bindings to the CPython2 C-API
Other
1.53k stars 138 forks source link

Compiling kw-args in tests #47

Open joeyism opened 7 years ago

joeyism commented 7 years ago

Hi,

I am trying to run the kw-args code in the tests folder. I ran go build which created the kw-args file. When I run ./kw-args I get the error could not import 'kwargs' Is there anything else I need to do for PyImport_ImportModule to import kwargs properly?

EDIT: I also tried #39 solution but that didn't work for me either

joeyism commented 7 years ago

I don't know if this is the proper way, especially because it seems like the code will be very dependent on work environment, but I was able to get it working with

sys_path := python.PySys_GetObject("path")

python.PyList_Append(sys_path, python.PyString_FromString("/home/test/Documents/Programming/go/src/github.com/sbinet/go-python/tests/kw-args"))

which is just $GOPATH/src/github.com/sbinet/go-python/tests/kw-args

This seems hacky though, is there a better way to do it?

sbinet commented 7 years ago

hi @joeyism

as you can see here: https://github.com/sbinet/go-python/blob/master/python_test.go#L26, the way these examples are run as tests is basically to add "." to $PYTHONPATH. Not sure that's something I would recommand for production, though :)

That said, I could add something to the various main.go package with instructions on how to run. Something like:

// Run like so:
//   $> PYTHONPATH=`pwd`:$PYTHONPATH \
//      go run ./main.go
package main
[...]

what do you think?