這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
閱讀原文請點擊
摘要: Python是時髦的機器學習禦用開發語言,Golang是大紅大紫的新時代後端開發語言。Python很適合讓搞演算法的寫寫模型,而Golang很適合提供API服務,兩位同志都紅的發紫,這裡就介紹一下正確攪基的辦法。 ,找到Python.h。一般linux直接安裝python-devel,mac直接用homebrew安裝就可以。
- Golang安裝:Golang不需要什麼特殊的處理,能找到
go即可。
安裝libpython-go-binding
雖然直接用cgo調用libpython也不是不可以,但是有native-binding用起來肯定要爽的多。Github上有一個現成的Binding庫go-python。
go get github.com/sbinet/go-python
如果Python安裝正確,這裡會自動編譯並顯示提示,事就這樣成了。
Have a try
首先寫一個測試Python指令碼
import numpyimport sklearna = 10def b(xixi): return xixi + "haha"
然後寫一個Go指令碼:
package mainimport ( "github.com/sbinet/go-python" "fmt")func init() { err := python.Initialize() if err != nil { panic(err.Error()) }}var PyStr = python.PyString_FromStringvar GoStr = python.PyString_AS_STRINGfunc main() { // import hello InsertBeforeSysPath("/Users/vonng/anaconda2/lib/python2.7/site-packages") hello := ImportModule("/Users/vonng/Dev/go/src/gitlab.alibaba-inc.com/cplus", "hello") fmt.Printf("[MODULE] repr(hello) = %s\n", GoStr(hello.Repr())) // print(hello.a) a := hello.GetAttrString("a") fmt.Printf("[VARS] a = %#v\n", python.PyInt_AsLong(a)) // print(hello.b) b := hello.GetAttrString("b") fmt.Printf("[FUNC] b = %#v\n", b) // args = tuple("xixi",) bArgs := python.PyTuple_New(1) python.PyTuple_SetItem(bArgs, 0, PyStr("xixi")) // b(*args) res := b.Call(bArgs, python.Py_None) fmt.Printf("[CALL] b('xixi') = %s\n", GoStr(res)) // sklearn sklearn := hello.GetAttrString("sklearn") skVersion := sklearn.GetAttrString("__version__") fmt.Printf("[IMPORT] sklearn = %s\n", GoStr(sklearn.Repr())) fmt.Printf("[IMPORT] sklearn version = %s\n", GoStr(skVersion.Repr()))}// InsertBeforeSysPath will add given dir to python import pathfunc InsertBeforeSysPath(p string) string { sysModule := python.PyImport_ImportModule("sys") path := sysModule.GetAttrString("path") python.PyList_Insert(path, 0, PyStr(p)) return GoStr(path.Repr())}// ImportModule will import python module from given directoryfunc ImportModule(dir, name string) *python.PyObject { sysModule := python.PyImport_ImportModule("sys") // import sys path := sysModule.GetAttrString("path") // path = sys.path python.PyList_Insert(path, 0, PyStr(dir)) // path.insert(0, dir) return python.PyImport_ImportModule(name) // return __import__(name)}
列印輸出為:
repr(hello) = <module 'hello' from '/Users/vonng/Dev/go/src/gitlab.alibaba-inc.com/cplus/hello.pyc'>a = 10b = &python.PyObject{ptr:(*python._Ctype_struct__object)(0xe90b1b8)}b('xixi') = xixihahasklearn = <module 'sklearn' from '/Users/vonng/anaconda2/lib/python2.7/site-packages/sklearn/__init__.pyc'>sklearn version = '0.18.1'
這裡簡單解釋一下。首先將這個指令碼的路徑添加到sys.path中。然後調用PyImport_ImportModule匯入包
使用GetAttrString可以根據屬性名稱擷取對象的屬性,相當於python中的.操作。調用Python函數可以採用Object.Call方法,,列表參數使用Tuple來構建。傳回值用PyString_AS_STRING從Python字串轉換為C或Go的字串。
更多用法可以參考Python-C API文檔。
但是只要有這幾個API,就足夠 Make python module rock & roll。充分利用Golang和Python各自的特性,構建靈活而強大的應用了。
閱讀原文請點擊