版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sureSand/article/details/82872909
我开始是在windows环境下直接go get github.com/sbinet/go-python
会报错pkg-config: exec: “pkg-config”: executable file not found in %PATH%
后来查网上资料无果,全是复制粘贴一个人的,我只好看官方文档和报错来解决问题。
官方文档写的是
If go get + pkg-config failed:
$ cd go-python
$ edit cgoflags.go
$make VERBOSE=1
Note: you’ll need the proper header and python
development environment. On Debian, you’ll need to install the
python-all-dev package
最后总结报这个错是/usr/lib/pkgconfig少了python-2.7.pc文件以及python-dev环境
在/usr/lib/pkgconfig新建python-2.7.pc:
prefix=/usr
exec_prefix=/usr
libdir=/usr/lib64
includedir=/usr/include
Name: Python
Description: Python library
Requires:
Version: 2.7
Libs.private: -lpthread -ldl -lutil
Libs: -L${libdir} -lpython2.7
Cflags: -I${includedir}/python2.7
直接下载python-dev会有依赖关系的问题,要通过aptitude来下载
sudo apt-get install aptitude
sudo aptitude install python-dev
最后下载go get github.com/sbinet/go-python,没有问题
运行一个官方例子
package main
import "fmt"
import "github.com/sbinet/go-python"
func init() {
err := python.Initialize()
if err != nil {
panic(err.Error())
}
}
func main() {
gostr := "foo"
pystr := python.PyString_FromString(gostr)
str := python.PyString_AsString(pystr)
fmt.Println("hello [", str, "]")
}
运行
$ go run ./main.go
输出
hello [ foo ]