{"id":935342,"date":"2024-12-26T18:51:36","date_gmt":"2024-12-26T10:51:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/935342.html"},"modified":"2024-12-26T18:51:38","modified_gmt":"2024-12-26T10:51:38","slug":"golang%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/935342.html","title":{"rendered":"golang\u5982\u4f55\u4f7f\u7528python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072013\/0116d657-145a-4334-99b2-0c0816b0ece3.webp\" alt=\"golang\u5982\u4f55\u4f7f\u7528python\" \/><\/p>\n<p><p> <strong>\u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528CGO\u8c03\u7528Python C API\u3001\u4f7f\u7528gopy\u5de5\u5177\u3001\u6216\u901a\u8fc7RPC\u8fdb\u884c\u901a\u4fe1\u7b49\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528CGO\u8c03\u7528Python\u7684C API\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u5728Go\u4e2d\u76f4\u63a5\u6267\u884cPython\u4ee3\u7801\u3001\u8c03\u7528Python\u51fd\u6570\u3001\u5e76\u4e0ePython\u5bf9\u8c61\u4ea4\u4e92\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u5e76\u63a2\u8ba8\u5176\u4ed6\u53ef\u884c\u7684\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001CGO\u8c03\u7528Python C API<\/p>\n<\/p>\n<p><p>CGO\u662f\u4e00\u79cd\u5141\u8bb8Go\u4ee3\u7801\u8c03\u7528C\u4ee3\u7801\u7684\u5de5\u5177\u3002\u5728Go\u4e2d\u4f7f\u7528Python C API\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u8bbe\u7f6ePython\u73af\u5883\u3001\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3001\u6267\u884cPython\u4ee3\u7801\u548c\u8c03\u7528Python\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6ePython\u73af\u5883<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528Python C API\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Python\u5f00\u53d1\u73af\u5883\u3002\u5177\u4f53\u800c\u8a00\uff0c\u9700\u8981\u5b89\u88c5Python\u7684\u5934\u6587\u4ef6\u548c\u5e93\u6587\u4ef6\uff0c\u8fd9\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5f00\u53d1\u5305\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5728Ubuntu\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3-dev<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Go\u4ee3\u7801\u4e2d\u4f7f\u7528Python\u4e4b\u524d\uff0c\u5fc5\u987b\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>Py_Initialize()<\/code>\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e00\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">\/*<\/p>\n<p>#cgo pkg-config: python3<\/p>\n<p>#include &lt;Python.h&gt;<\/p>\n<p>*\/<\/p>\n<p>import &quot;C&quot;<\/p>\n<p>func m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    C.Py_Initialize()<\/p>\n<p>    defer C.Py_Finalize()<\/p>\n<p>    \/\/ \u5176\u4ed6Python\u8c03\u7528\u4ee3\u7801<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>#cgo pkg-config: python3<\/code>\u6307\u793aCGO\u4f7f\u7528Python 3\u7684\u7f16\u8bd1\u6807\u5fd7\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6267\u884cPython\u4ee3\u7801<\/strong><\/li>\n<\/ol>\n<p><p>\u521d\u59cb\u5316\u89e3\u91ca\u5668\u540e\uff0c\u53ef\u4ee5\u6267\u884c\u4efb\u610f\u7684Python\u4ee3\u7801\u3002\u53ef\u4ee5\u4f7f\u7528<code>PyRun_SimpleString()<\/code>\u51fd\u6570\u76f4\u63a5\u6267\u884cPython\u4ee3\u7801\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">code := `<\/p>\n<p>import sys<\/p>\n<p>print(sys.version)<\/p>\n<p>`<\/p>\n<p>C.PyRun_SimpleString(C.CString(code))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5728Go\u7a0b\u5e8f\u4e2d\u6253\u5370\u51fa\u5f53\u524dPython\u89e3\u91ca\u5668\u7684\u7248\u672c\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u8c03\u7528Python\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7Python C API\uff0c\u53ef\u4ee5\u4eceGo\u8c03\u7528\u7279\u5b9a\u7684Python\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u5982\u4e0bPython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728Go\u4e2d\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">\/\/ \u83b7\u53d6__main__\u6a21\u5757<\/p>\n<p>mainModule := C.PyImport_AddModule(C.CString(&quot;__main__&quot;))<\/p>\n<p>globalDict := C.PyModule_GetDict(mainModule)<\/p>\n<p>\/\/ \u5b9a\u4e49Python\u4ee3\u7801<\/p>\n<p>code := `<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>`<\/p>\n<p>C.PyRun_SimpleString(C.CString(code))<\/p>\n<p>\/\/ \u83b7\u53d6\u51fd\u6570\u5bf9\u8c61<\/p>\n<p>addFunc := C.PyDict_GetItemString(globalDict, C.CString(&quot;add&quot;))<\/p>\n<p>\/\/ \u8c03\u7528\u51fd\u6570<\/p>\n<p>args := C.PyTuple_Pack(2, C.PyLong_FromLong(3), C.PyLong_FromLong(5))<\/p>\n<p>result := C.PyObject_CallObject(addFunc, args)<\/p>\n<p>fmt.Printf(&quot;Result: %d\\n&quot;, C.PyLong_AsLong(result))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06Python\u51fd\u6570\u52a0\u8f7d\u5230Go\u4e2d\uff0c\u7136\u540e\u901a\u8fc7<code>PyObject_CallObject()<\/code>\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528gopy\u5de5\u5177<\/p>\n<\/p>\n<p><p>gopy\u662f\u4e00\u4e2a\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Go\u5305\u8f6c\u6362\u4e3a\u53ef\u4f9bPython\u8c03\u7528\u7684\u6a21\u5757\u3002\u4f7f\u7528gopy\u53ef\u4ee5\u8ba9Python\u76f4\u63a5\u8c03\u7528Go\u7684\u51fd\u6570\u548c\u7c7b\u578b\uff0c\u8fd9\u9002\u7528\u4e8e\u5e0c\u671b\u5728Python\u4e2d\u5d4c\u5165Go\u903b\u8f91\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5gopy<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5gopy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">go install github.com\/go-python\/gopy@latest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528gopy\u751f\u6210Python\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u7b80\u5355\u7684Go\u5305\uff0c\u5305\u542b\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u52a0\u6cd5\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">package main<\/p>\n<p>\/\/ Add returns the sum of two integers<\/p>\n<p>func Add(a, b int) int {<\/p>\n<p>    return a + b<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528gopy\u751f\u6210\u5bf9\u5e94\u7684Python\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">gopy bind -lang=py3 .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2aPython\u6a21\u5757\uff0cPython\u7a0b\u5e8f\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u5e76\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u901a\u8fc7RPC\u8fdb\u884c\u901a\u4fe1<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5728Go\u548cPython\u4e4b\u95f4\u4ea4\u4e92\u7684\u65b9\u5f0f\u662f\u4f7f\u7528RPC\uff08\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff09\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u5728\u4e0d\u540c\u8fdb\u7a0b\u6216\u4e0d\u540c\u673a\u5668\u4e0a\u8fd0\u884c\u7684Go\u548cPython\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49RPC\u670d\u52a1<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\u5728Go\u4e2d\u5b9a\u4e49\u4e00\u4e2aRPC\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">package main<\/p>\n<p>import (<\/p>\n<p>    &quot;net&quot;<\/p>\n<p>    &quot;net\/rpc&quot;<\/p>\n<p>)<\/p>\n<p>type Args struct {<\/p>\n<p>    A, B int<\/p>\n<p>}<\/p>\n<p>type Arith int<\/p>\n<p>func (t *Arith) Multiply(args *Args, reply *int) error {<\/p>\n<p>    *reply = args.A * args.B<\/p>\n<p>    return nil<\/p>\n<p>}<\/p>\n<p>func main() {<\/p>\n<p>    arith := new(Arith)<\/p>\n<p>    rpc.Register(arith)<\/p>\n<p>    listener, _ := net.Listen(&quot;tcp&quot;, &quot;:1234&quot;)<\/p>\n<p>    defer listener.Close()<\/p>\n<p>    for {<\/p>\n<p>        conn, _ := listener.Accept()<\/p>\n<p>        go rpc.ServeConn(conn)<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5728Python\u4e2d\u8c03\u7528RPC\u670d\u52a1<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Python\u7684<code>xmlrpc.client<\/code>\u6a21\u5757\u8c03\u7528Go\u7684RPC\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xmlrpc.client<\/p>\n<p>proxy = xmlrpc.client.ServerProxy(&quot;http:\/\/localhost:1234\/&quot;)<\/p>\n<p>result = proxy.Multiply({&#39;A&#39;: 7, &#39;B&#39;: 6})<\/p>\n<p>print(&quot;7 * 6 = %d&quot; % result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\uff0c\u9002\u5408\u9700\u8981\u8de8\u8bed\u8a00\u3001\u8de8\u5e73\u53f0\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Go\u4e2d\u4f7f\u7528Python\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5bf9\u4e8e\u9700\u8981\u76f4\u63a5\u8c03\u7528Python\u51fd\u6570\u7684\u573a\u666f\uff0c\u4f7f\u7528CGO\u548cPython C API\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff1b\u800c\u5bf9\u4e8e\u9700\u8981\u5728Python\u4e2d\u4f7f\u7528Go\u903b\u8f91\u7684\u573a\u666f\uff0cgopy\u5de5\u5177\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u89e3\u51b3\u65b9\u6848\uff1b\u5bf9\u4e8e\u9700\u8981\u8de8\u8fdb\u7a0b\u6216\u8de8\u5e73\u53f0\u7684\u573a\u666f\uff0cRPC\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u901a\u4fe1\u673a\u5236\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Golang\u4e2d\u8c03\u7528Python\u811a\u672c\uff1f<\/strong><br \/>\u5728Golang\u4e2d\u8c03\u7528Python\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u201cos\/exec\u201d\u5305\u6765\u6267\u884cPython\u811a\u672c\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u3002\u63a5\u7740\uff0c\u5728Golang\u4ee3\u7801\u4e2d\u4f7f\u7528<code>exec.Command<\/code>\u51fd\u6570\u6765\u8c03\u7528Python\u811a\u672c\uff0c\u4f20\u9012\u4efb\u4f55\u5fc5\u8981\u7684\u53c2\u6570\u3002\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6807\u51c6\u8f93\u5165\u548c\u8f93\u51fa\u4e0ePython\u811a\u672c\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<p><strong>Golang\u4e0ePython\u7684\u4ea4\u4e92\u6709\u54ea\u4e9b\u5e93\u53ef\u4f9b\u4f7f\u7528\uff1f<\/strong><br \/>\u4e3a\u4e86\u5b9e\u73b0Golang\u4e0ePython\u7684\u4ea4\u4e92\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u73b0\u6210\u7684\u5e93\u3002\u6bd4\u5982\uff0c\u201cgo-python\u201d\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5e93\uff0c\u5b83\u5141\u8bb8\u5728Golang\u4e2d\u76f4\u63a5\u5d4c\u5165Python\u89e3\u91ca\u5668\uff0c\u652f\u6301\u8c03\u7528Python\u51fd\u6570\u548c\u5bf9\u8c61\u3002\u6b64\u5916\uff0c\u201cgopy\u201d\u4e5f\u63d0\u4f9b\u4e86\u5c06Python\u4ee3\u7801\u8f6c\u5316\u4e3aGo\u6a21\u5757\u7684\u529f\u80fd\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u5728Golang\u4e2d\u4f7f\u7528Python\u7684\u7279\u6027\u548c\u5e93\u3002<\/p>\n<p><strong>\u5728Golang\u4e2d\u5982\u4f55\u5904\u7406Python\u8fd4\u56de\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5f53\u5728Golang\u4e2d\u8c03\u7528Python\u811a\u672c\u65f6\uff0c\u5904\u7406\u8fd4\u56de\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u6807\u51c6\u8f93\u51fa\u5b9e\u73b0\u3002\u5982\u679c\u4f60\u7684Python\u811a\u672c\u8f93\u51fa\u7ed3\u679c\u5230\u6807\u51c6\u8f93\u51fa\uff0c\u53ef\u4ee5\u5728Golang\u4e2d\u901a\u8fc7<code>cmd.Output()<\/code>\u6216<code>cmd.CombinedOutput()<\/code>\u65b9\u6cd5\u83b7\u53d6\u8fd4\u56de\u7ed3\u679c\uff0c\u5e76\u5c06\u5176\u8f6c\u5316\u4e3a\u6240\u9700\u7684\u6570\u636e\u683c\u5f0f\u3002\u6839\u636e\u6570\u636e\u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528JSON\u89e3\u6790\u6216\u5b57\u7b26\u4e32\u5904\u7406\u7684\u65b9\u5f0f\u6765\u63d0\u53d6\u548c\u5229\u7528\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528CGO\u8c03\u7528Python C API\u3001\u4f7f\u7528gopy\u5de5\u5177\u3001 [&hellip;]","protected":false},"author":3,"featured_media":935347,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935342"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=935342"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935342\/revisions"}],"predecessor-version":[{"id":935350,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/935342\/revisions\/935350"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/935347"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=935342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=935342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=935342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}