目前,go-python3的sdk是使用,https://github.com/DataDog/go-python3
。
注意⚠️:环境只能在python3.7,否则会报错。
conda安装后,直接 conda create -n pygo python=3.7
。
go代码(main.go)
package main
import "github.com/DataDog/go-python3"
func main() {
defer python3.Py_Finalize()
python3.Py_Initialize()
python3.PyRun_SimpleString("print('hello from python in go')")
}
然后执行:
go mod tiny
go run main.go
错误信息:
go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: cannot find -lpython3.7m
collect2: error: ld returned 1 exit status
解决方案:
rm -rf ~/.cache/go-build
export CXXFLAGS="-stdlib=libstdc++" CC=/usr/bin/gcc CXX=/usr/bin/g++
接着报错,报错信息
export PKG_CONFIG_PATH=gopy(你自己创建的虚拟环境路径)/lib/pkgconfig
go get github.com/DataDog/go-python3
go mod tidy
go run main.go
hello from python in go
over