go呼叫python報錯pkg-config: exec: "pkg-config": executable file not found in %PATH%

lightTrace發表於2018-09-27

我開始是在windows環境下直接go get github.com/sbinet/go-python
會報錯pkg-config: exec: “pkg-config”: executable file not found in %PATH%

後來還是算了,直接在我的雲伺服器上面去跑了,windows系統的坑略多,於是放棄windows,直接去了linux系統進行如下操作:

官方文件寫的是

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 ]

相關文章