golang 调用 python

Golang中调用Python3 - 知乎
2.10. Go实现Python模块-Go语言高级编程

golang和python互相调用 - dhcn - 博客园

Golang调用Python - Go语言中文网 - Golang中文社区

golang调用python实战-阿里云开发者社区

go执行python/shell脚本 | 航行学园

python代码

安装pip2.7等参考

ubuntu安装python_sun007700的专栏-CSDN博客

import os
import numpy
import sklearn

import sys

print(sys.version)

dir = os.path.dirname(os.__file__)


print(os.__file__)
print(dir)

a = 10

def b(xixi):
    return xixi + "haha"

m = b("nihao")

print(m)

go代码

package main

import (
	"fmt"

	"github.com/sbinet/go-python"
)

func init() {
	err := python.Initialize()
	if err != nil {
		panic(err.Error())
	}
}

var PyStr = python.PyString_FromString
var GoStr = python.PyString_AS_STRING

func main() {
	// import hello
	//InsertBeforeSysPath("/Users/vonng/anaconda2/lib/python2.7/site-packages")
	InsertBeforeSysPath("/usr/local/lib/python2.7/site-packages")
	//	hello := ImportModule("/Users/vonng/Dev/go/src/gitlab.alibaba-inc.com/cplus", "hello")
	hello := ImportModule("/home/test/python/test", "test")
	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 path
func 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 directory
func 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)
}

// package main

// import (
// 	"fmt"

// 	"github.com/sbinet/go-python"
// )

// // 初始化go-python
// func init() {
// 	err := python.Initialize()
// 	if err != nil {
// 		panic(err.Error())
// 	}
// }

// func main() {
// 	gostr := "foo"                             //定义goloang字符串
// 	pystr := python.PyString_FromString(gostr) //将golang字符串专程python字符串
// 	str := python.PyString_AsString(pystr)     //将python字符串,再转为golang字符串。
// 	fmt.Println("hello [", str, "]")

// 	pickle := python.PyImport_ImportModule("cPickle") //导入cPickle模块
// 	if pickle == nil {
// 		panic("could not import 'cPickle'")
// 	}

// 	dumps := pickle.GetAttrString("dumps") //获取dumps函数
// 	if dumps == nil {
// 		panic("could not retrieve 'cPickle.dumps'")
// 	}
// 	defer dumps.DecRef() //减少引用计数,释放资源。

// 	out := dumps.CallFunctionObjArgs("O", pystr) //针对python字符串进行dumps操作。
// 	if out == nil {
// 		panic("could not dump pystr")
// 	}
// 	defer out.DecRef()

// 	fmt.Printf("cPickle.dumps(%s) = %q\n", gostr,
// 		python.PyString_AsString(out),
// 	)

// 	loads := pickle.GetAttrString("loads") //获取loads函数
// 	if loads == nil {
// 		panic("could not retrieve 'cPickle.loads'")
// 	}
// 	defer loads.DecRef()
// 	out2 := loads.CallFunctionObjArgs("O", out) //将dumps结果重新loads
// 	if out2 == nil {
// 		panic("could not load back out")
// 	}
// 	defer out2.DecRef()

// 	fmt.Printf("cPickle.loads(%q) = %q\n",
// 		python.PyString_AsString(out),
// 		python.PyString_AsString(out2),
// 	)
// }


golang调用python实战-阿里云开发者社区

go get github.com/DataDog/go-python3

pkg-config --list-all
locate   python3.pc


……
apt-get install aptitude
aptitude remove python3.7-dev
aptitude remove python3-dev
aptitude remove python-dev
……



could not determine kind of name for C.PyImport_GetModule · Issue #19 · DataDog/go-python3 · GitHub

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值