{"id":1184044,"date":"2025-01-15T19:21:40","date_gmt":"2025-01-15T11:21:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1184044.html"},"modified":"2025-01-15T19:21:43","modified_gmt":"2025-01-15T11:21:43","slug":"c%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e6%89%a7%e8%a1%8cpython%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1184044.html","title":{"rendered":"c\u8bed\u8a00\u5982\u4f55\u6267\u884cpython\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133601\/babdf1bf-8fd6-4f02-b596-379601c6c45e.webp\" alt=\"c\u8bed\u8a00\u5982\u4f55\u6267\u884cpython\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>C\u8bed\u8a00\u6267\u884cPython\u4ee3\u7801\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4f7f\u7528Python\/C API\u3001\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u3001\u5d4c\u5165Python\u89e3\u91ca\u5668<\/strong>\u3002\u5176\u4e2d\uff0c<strong>Python\/C API<\/strong> \u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8C\u7a0b\u5e8f\u8c03\u7528Python\u4ee3\u7801\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>Python\/C API \u662f\u4e00\u4e2aC\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u51fd\u6570\u548c\u5b8f\uff0c\u7528\u4e8e\u5728C\u7a0b\u5e8f\u4e2d\u5d4c\u5165Python\u89e3\u91ca\u5668\u3001\u6267\u884cPython\u4ee3\u7801\u3001\u64cd\u4f5cPython\u5bf9\u8c61\u3002\u8981\u4f7f\u7528Python\/C API\uff0c\u9700\u8981\u5305\u542bPython.h\u5934\u6587\u4ef6\u5e76\u94fe\u63a5Python\u5e93\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u8981\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\/C API\u5728C\u7a0b\u5e8f\u4e2d\u6267\u884cPython\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ \u6267\u884c\u7b80\u5355\u7684Python\u4ee3\u7801<\/p>\n<p>    PyRun_SimpleString(&quot;print(&#39;Hello from Python!&#39;)&quot;);<\/p>\n<p>    \/\/ \u7ed3\u675fPython\u89e3\u91ca\u5668<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728C\u7a0b\u5e8f\u4e2d\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3001\u6267\u884c\u7b80\u5355\u7684Python\u4ee3\u7801\u5e76\u6700\u7ec8\u5173\u95edPython\u89e3\u91ca\u5668\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528Python\/C API\u5728C\u8bed\u8a00\u4e2d\u6267\u884c\u66f4\u590d\u6742\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\/C API\u4e4b\u524d\uff0c\u9700\u8981\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3002\u53ef\u4ee5\u4f7f\u7528<code>Py_Initialize<\/code>\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e2a\u64cd\u4f5c\u3002\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u540e\uff0cC\u7a0b\u5e8f\u53ef\u4ee5\u8c03\u7528Python\u4ee3\u7801\u548c\u4f7f\u7528Python\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ \u6267\u884c\u7b80\u5355\u7684Python\u4ee3\u7801<\/p>\n<p>    PyRun_SimpleString(&quot;print(&#39;Hello from Python!&#39;)&quot;);<\/p>\n<p>    \/\/ \u7ed3\u675fPython\u89e3\u91ca\u5668<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Py_Initialize<\/code>\u51fd\u6570\u88ab\u8c03\u7528\u6765\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u6267\u884cPython\u4ee3\u7801<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528PyRun_SimpleString\u51fd\u6570\u6267\u884c\u7b80\u5355Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><p><code>PyRun_SimpleString<\/code>\u51fd\u6570\u5141\u8bb8C\u7a0b\u5e8f\u6267\u884c\u4e00\u6bb5\u7b80\u5355\u7684Python\u4ee3\u7801\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2aC\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3aPython\u4ee3\u7801\u6765\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ \u6267\u884c\u7b80\u5355\u7684Python\u4ee3\u7801<\/p>\n<p>    PyRun_SimpleString(&quot;print(&#39;Hello from Python!&#39;)&quot;);<\/p>\n<p>    \/\/ \u7ed3\u675fPython\u89e3\u91ca\u5668<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>PyRun_SimpleString<\/code>\u51fd\u6570\u88ab\u7528\u6765\u6267\u884c\u4e00\u6bb5\u7b80\u5355\u7684Python\u4ee3\u7801\uff0c\u8be5\u4ee3\u7801\u53ea\u662f\u6253\u5370\u4e86\u4e00\u6761\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528PyRun_String\u51fd\u6570\u6267\u884c\u590d\u6742Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><p><code>PyRun_String<\/code>\u51fd\u6570\u5141\u8bb8C\u7a0b\u5e8f\u6267\u884c\u66f4\u590d\u6742\u7684Python\u4ee3\u7801\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2aC\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3aPython\u4ee3\u7801\u6765\u6267\u884c\u3002\u4e0e<code>PyRun_SimpleString<\/code>\u4e0d\u540c\uff0c<code>PyRun_String<\/code>\u5141\u8bb8\u6307\u5b9a\u6267\u884c\u73af\u5883\uff0c\u5305\u62ec\u5168\u5c40\u548c\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u5168\u5c40\u548c\u5c40\u90e8\u547d\u540d\u7a7a\u95f4<\/p>\n<p>    PyObject *global_dict = PyDict_New();<\/p>\n<p>    PyObject *local_dict = PyDict_New();<\/p>\n<p>    \/\/ \u6267\u884c\u590d\u6742\u7684Python\u4ee3\u7801<\/p>\n<p>    PyObject *result = PyRun_String(<\/p>\n<p>        &quot;x = 10\\n&quot;<\/p>\n<p>        &quot;y = 20\\n&quot;<\/p>\n<p>        &quot;z = x + y\\n&quot;<\/p>\n<p>        &quot;print(z)&quot;,<\/p>\n<p>        Py_file_input, global_dict, local_dict<\/p>\n<p>    );<\/p>\n<p>    \/\/ \u68c0\u67e5\u6267\u884c\u7ed3\u679c<\/p>\n<p>    if (result == NULL) {<\/p>\n<p>        PyErr_Print();<\/p>\n<p>    } else {<\/p>\n<p>        Py_DECREF(result);<\/p>\n<p>    }<\/p>\n<p>    \/\/ \u91ca\u653e\u547d\u540d\u7a7a\u95f4<\/p>\n<p>    Py_DECREF(global_dict);<\/p>\n<p>    Py_DECREF(local_dict);<\/p>\n<p>    \/\/ \u7ed3\u675fPython\u89e3\u91ca\u5668<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>PyRun_String<\/code>\u51fd\u6570\u88ab\u7528\u6765\u6267\u884c\u4e00\u6bb5\u66f4\u590d\u6742\u7684Python\u4ee3\u7801\uff0c\u8be5\u4ee3\u7801\u8ba1\u7b97\u4e24\u4e2a\u53d8\u91cf\u7684\u548c\u5e76\u6253\u5370\u7ed3\u679c\u3002\u5168\u5c40\u548c\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u88ab\u8bbe\u7f6e\u4e3a\u65b0\u7684\u5b57\u5178\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u8c03\u7528Python\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b9a\u4e49\u5e76\u8c03\u7528Python\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728C\u7a0b\u5e8f\u4e2d\u5b9a\u4e49Python\u51fd\u6570\u5e76\u8c03\u7528\u5b83\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>PyRun_SimpleString<\/code>\u6216<code>PyRun_String<\/code>\u5b9a\u4e49\u4e00\u4e2aPython\u51fd\u6570\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>PyObject_CallObject<\/code>\u51fd\u6570\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ \u5b9a\u4e49Python\u51fd\u6570<\/p>\n<p>    PyRun_SimpleString(<\/p>\n<p>        &quot;def add(x, y):\\n&quot;<\/p>\n<p>        &quot;    return x + y\\n&quot;<\/p>\n<p>    );<\/p>\n<p>    \/\/ \u83b7\u53d6\u5168\u5c40\u547d\u540d\u7a7a\u95f4<\/p>\n<p>    PyObject *global_dict = PyEval_GetGlobals();<\/p>\n<p>    \/\/ \u83b7\u53d6\u51fd\u6570\u5bf9\u8c61<\/p>\n<p>    PyObject *add_func = PyDict_GetItemString(global_dict, &quot;add&quot;);<\/p>\n<p>    \/\/ \u521b\u5efa\u53c2\u6570\u5143\u7ec4<\/p>\n<p>    PyObject *args = PyTuple_Pack(2, PyLong_FromLong(10), PyLong_FromLong(20));<\/p>\n<p>    \/\/ \u8c03\u7528\u51fd\u6570<\/p>\n<p>    PyObject *result = PyObject_CallObject(add_func, args);<\/p>\n<p>    \/\/ \u68c0\u67e5\u8c03\u7528\u7ed3\u679c<\/p>\n<p>    if (result == NULL) {<\/p>\n<p>        PyErr_Print();<\/p>\n<p>    } else {<\/p>\n<p>        long sum = PyLong_AsLong(result);<\/p>\n<p>        printf(&quot;Result: %ld\\n&quot;, sum);<\/p>\n<p>        Py_DECREF(result);<\/p>\n<p>    }<\/p>\n<p>    \/\/ \u91ca\u653e\u53c2\u6570\u5143\u7ec4<\/p>\n<p>    Py_DECREF(args);<\/p>\n<p>    \/\/ \u7ed3\u675fPython\u89e3\u91ca\u5668<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u901a\u8fc7<code>PyRun_SimpleString<\/code>\u5b9a\u4e49\u4e86\u4e00\u4e2aPython\u51fd\u6570<code>add<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>PyDict_GetItemString<\/code>\u4ece\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u83b7\u53d6\u8be5\u51fd\u6570\u7684\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u53c2\u6570\u5143\u7ec4\u5e76\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u6700\u540e\u68c0\u67e5\u8c03\u7528\u7ed3\u679c\u5e76\u6253\u5370\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5904\u7406Python\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u8c03\u7528Python\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230Python\u5f02\u5e38\u3002\u53ef\u4ee5\u4f7f\u7528<code>PyErr_Occurred<\/code>\u51fd\u6570\u68c0\u67e5\u662f\u5426\u53d1\u751f\u4e86\u5f02\u5e38\uff0c\u5e76\u4f7f\u7528<code>PyErr_Print<\/code>\u51fd\u6570\u6253\u5370\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ \u5b9a\u4e49Python\u51fd\u6570<\/p>\n<p>    PyRun_SimpleString(<\/p>\n<p>        &quot;def divide(x, y):\\n&quot;<\/p>\n<p>        &quot;    return x \/ y\\n&quot;<\/p>\n<p>    );<\/p>\n<p>    \/\/ \u83b7\u53d6\u5168\u5c40\u547d\u540d\u7a7a\u95f4<\/p>\n<p>    PyObject *global_dict = PyEval_GetGlobals();<\/p>\n<p>    \/\/ \u83b7\u53d6\u51fd\u6570\u5bf9\u8c61<\/p>\n<p>    PyObject *divide_func = PyDict_GetItemString(global_dict, &quot;divide&quot;);<\/p>\n<p>    \/\/ \u521b\u5efa\u53c2\u6570\u5143\u7ec4<\/p>\n<p>    PyObject *args = PyTuple_Pack(2, PyLong_FromLong(10), PyLong_FromLong(0));<\/p>\n<p>    \/\/ \u8c03\u7528\u51fd\u6570<\/p>\n<p>    PyObject *result = PyObject_CallObject(divide_func, args);<\/p>\n<p>    \/\/ \u68c0\u67e5\u8c03\u7528\u7ed3\u679c<\/p>\n<p>    if (result == NULL) {<\/p>\n<p>        if (PyErr_Occurred()) {<\/p>\n<p>            PyErr_Print();<\/p>\n<p>        }<\/p>\n<p>    } else {<\/p>\n<p>        long quotient = PyLong_AsLong(result);<\/p>\n<p>        printf(&quot;Result: %ld\\n&quot;, quotient);<\/p>\n<p>        Py_DECREF(result);<\/p>\n<p>    }<\/p>\n<p>    \/\/ \u91ca\u653e\u53c2\u6570\u5143\u7ec4<\/p>\n<p>    Py_DECREF(args);<\/p>\n<p>    \/\/ \u7ed3\u675fPython\u89e3\u91ca\u5668<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8c03\u7528<code>divide<\/code>\u51fd\u6570\u65f6\u4f20\u5165\u4e86\u9664\u6570\u4e3a\u96f6\u7684\u53c2\u6570\u3002\u7531\u4e8ePython\u4e2d\u9664\u4ee5\u96f6\u4f1a\u5f15\u53d1\u5f02\u5e38\uff0c\u56e0\u6b64\u4f7f\u7528<code>PyErr_Occurred<\/code>\u51fd\u6570\u68c0\u67e5\u662f\u5426\u53d1\u751f\u4e86\u5f02\u5e38\uff0c\u5e76\u4f7f\u7528<code>PyErr_Print<\/code>\u51fd\u6570\u6253\u5370\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u6267\u884cPython\u811a\u672c<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528Python\/C API\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u5728C\u7a0b\u5e8f\u4e2d\u6267\u884cPython\u811a\u672c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6267\u884c\u72ec\u7acb\u7684Python\u811a\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdlib.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u6267\u884cPython\u811a\u672c<\/p>\n<p>    int result = system(&quot;python script.py&quot;);<\/p>\n<p>    \/\/ \u68c0\u67e5\u6267\u884c\u7ed3\u679c<\/p>\n<p>    if (result == -1) {<\/p>\n<p>        printf(&quot;Failed to execute script\\n&quot;);<\/p>\n<p>    } else {<\/p>\n<p>        printf(&quot;Script executed successfully\\n&quot;);<\/p>\n<p>    }<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>system<\/code>\u51fd\u6570\u6267\u884c\u540d\u4e3a<code>script.py<\/code>\u7684Python\u811a\u672c\u6587\u4ef6\u3002\u68c0\u67e5<code>system<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4ee5\u786e\u5b9a\u811a\u672c\u662f\u5426\u6210\u529f\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5d4c\u5165Python\u89e3\u91ca\u5668<\/h2>\n<\/p>\n<p><p>\u5d4c\u5165Python\u89e3\u91ca\u5668\u662f\u6307\u5728C\u7a0b\u5e8f\u4e2d\u5d4c\u5165\u4e00\u4e2a\u5b8c\u6574\u7684Python\u89e3\u91ca\u5668\uff0c\u5e76\u4f7f\u7528\u8be5\u89e3\u91ca\u5668\u6267\u884cPython\u4ee3\u7801\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u66f4\u7075\u6d3b\u5730\u6267\u884cPython\u4ee3\u7801\uff0c\u5e76\u4e0eC\u7a0b\u5e8f\u8fdb\u884c\u66f4\u590d\u6742\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ \u6267\u884cPython\u4ee3\u7801<\/p>\n<p>    PyRun_SimpleString(<\/p>\n<p>        &quot;import sys\\n&quot;<\/p>\n<p>        &quot;sys.path.append(&#39;.&#39;)\\n&quot;<\/p>\n<p>        &quot;import script\\n&quot;<\/p>\n<p>    );<\/p>\n<p>    \/\/ \u7ed3\u675fPython\u89e3\u91ca\u5668<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>PyRun_SimpleString<\/code>\u6267\u884cPython\u4ee3\u7801\uff0c\u5bfc\u5165\u540d\u4e3a<code>script<\/code>\u7684Python\u6a21\u5757\u3002\u6700\u540e\uff0c\u5173\u95edPython\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4e0ePython\u5bf9\u8c61\u4ea4\u4e92<\/h2>\n<\/p>\n<p><h3>1\u3001\u521b\u5efa\u548c\u64cd\u4f5cPython\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728C\u7a0b\u5e8f\u4e2d\u521b\u5efa\u548c\u64cd\u4f5cPython\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efaPython\u5217\u8868\u3001\u5b57\u5178\u548c\u5143\u7ec4\uff0c\u5e76\u4f7f\u7528Python\/C API\u63d0\u4f9b\u7684\u51fd\u6570\u64cd\u4f5c\u8fd9\u4e9b\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ \u521b\u5efaPython\u5217\u8868<\/p>\n<p>    PyObject *list = PyList_New(0);<\/p>\n<p>    \/\/ \u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20<\/p>\n<p>    PyList_Append(list, PyLong_FromLong(10));<\/p>\n<p>    PyList_Append(list, PyLong_FromLong(20));<\/p>\n<p>    PyList_Append(list, PyLong_FromLong(30));<\/p>\n<p>    \/\/ \u83b7\u53d6\u5217\u8868\u957f\u5ea6<\/p>\n<p>    Py_ssize_t length = PyList_Size(list);<\/p>\n<p>    printf(&quot;List length: %zd\\n&quot;, length);<\/p>\n<p>    \/\/ \u83b7\u53d6\u5e76\u6253\u5370\u5217\u8868\u5143\u7d20<\/p>\n<p>    for (Py_ssize_t i = 0; i &lt; length; i++) {<\/p>\n<p>        PyObject *item = PyList_GetItem(list, i);<\/p>\n<p>        long value = PyLong_AsLong(item);<\/p>\n<p>        printf(&quot;List item %zd: %ld\\n&quot;, i, value);<\/p>\n<p>    }<\/p>\n<p>    \/\/ \u91ca\u653e\u5217\u8868\u5bf9\u8c61<\/p>\n<p>    Py_DECREF(list);<\/p>\n<p>    \/\/ \u7ed3\u675fPython\u89e3\u91ca\u5668<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684Python\u5217\u8868\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>PyList_Append<\/code>\u51fd\u6570\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>PyList_Size<\/code>\u51fd\u6570\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff0c\u5e76\u4f7f\u7528<code>PyList_GetItem<\/code>\u51fd\u6570\u83b7\u53d6\u5e76\u6253\u5370\u5217\u8868\u5143\u7d20\u3002\u6700\u540e\uff0c\u91ca\u653e\u5217\u8868\u5bf9\u8c61\u5e76\u5173\u95edPython\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u8f6c\u6362C\u6570\u636e\u7c7b\u578b\u548cPython\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728C\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u5c06C\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06C\u7684\u6574\u6570\u3001\u6d6e\u70b9\u6570\u548c\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684Python\u5bf9\u8c61\uff0c\u5e76\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aC\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    \/\/ \u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ C\u6574\u6570\u8f6c\u6362\u4e3aPython\u5bf9\u8c61<\/p>\n<p>    int c_int = 42;<\/p>\n<p>    PyObject *py_int = PyLong_FromLong(c_int);<\/p>\n<p>    \/\/ Python\u5bf9\u8c61\u8f6c\u6362\u4e3aC\u6574\u6570<\/p>\n<p>    long result_int = PyLong_AsLong(py_int);<\/p>\n<p>    printf(&quot;C integer: %d, Python integer: %ld\\n&quot;, c_int, result_int);<\/p>\n<p>    \/\/ C\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61<\/p>\n<p>    const char *c_str = &quot;Hello, World!&quot;;<\/p>\n<p>    PyObject *py_str = PyUnicode_FromString(c_str);<\/p>\n<p>    \/\/ Python\u5bf9\u8c61\u8f6c\u6362\u4e3aC\u5b57\u7b26\u4e32<\/p>\n<p>    const char *result_str = PyUnicode_AsUTF8(py_str);<\/p>\n<p>    printf(&quot;C string: %s, Python string: %s\\n&quot;, c_str, result_str);<\/p>\n<p>    \/\/ \u91ca\u653ePython\u5bf9\u8c61<\/p>\n<p>    Py_DECREF(py_int);<\/p>\n<p>    Py_DECREF(py_str);<\/p>\n<p>    \/\/ \u7ed3\u675fPython\u89e3\u91ca\u5668<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u5c06C\u7684\u6574\u6570\u548c\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684Python\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u56deC\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u6253\u5370\u7ed3\u679c\u3002\u6700\u540e\uff0c\u91ca\u653ePython\u5bf9\u8c61\u5e76\u5173\u95edPython\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Python\/C API\u3001\u7cfb\u7edf\u8c03\u7528\u548c\u5d4c\u5165Python\u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u5728C\u7a0b\u5e8f\u4e2d\u6267\u884cPython\u4ee3\u7801\u3002Python\/C API \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u5b8f\uff0c\u7528\u4e8e\u5728C\u7a0b\u5e8f\u4e2d\u5d4c\u5165Python\u89e3\u91ca\u5668\u3001\u6267\u884cPython\u4ee3\u7801\u3001\u64cd\u4f5cPython\u5bf9\u8c61\u3002\u7cfb\u7edf\u8c03\u7528\u9002\u7528\u4e8e\u6267\u884c\u72ec\u7acb\u7684Python\u811a\u672c\u6587\u4ef6\uff0c\u800c\u5d4c\u5165Python\u89e3\u91ca\u5668\u5141\u8bb8\u66f4\u7075\u6d3b\u5730\u6267\u884cPython\u4ee3\u7801\uff0c\u5e76\u4e0eC\u7a0b\u5e8f\u8fdb\u884c\u66f4\u590d\u6742\u7684\u4ea4\u4e92\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728C\u7a0b\u5e8f\u4e2d\u5145\u5206\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u5b9e\u73b0\u590d\u6742\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728C\u8bed\u8a00\u4e2d\u8c03\u7528Python\u4ee3\u7801\uff1f<\/strong><br \/>\u5728C\u8bed\u8a00\u4e2d\u8c03\u7528Python\u4ee3\u7801\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684C API\u6765\u5b9e\u73b0\u3002\u8fd9\u9700\u8981\u5728C\u4ee3\u7801\u4e2d\u5f15\u5165Python.h\u5934\u6587\u4ef6\uff0c\u5e76\u94fe\u63a5Python\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Py_Initialize()\u51fd\u6570\u6765\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\uff0c\u63a5\u7740\u4f7f\u7528PyRun_SimpleString()\u6216\u5176\u4ed6API\u51fd\u6570\u6765\u6267\u884cPython\u4ee3\u7801\u3002\u786e\u4fdd\u5728\u7f16\u8bd1\u65f6\u94fe\u63a5Python\u5e93\uff0c\u4f8b\u5982\u4f7f\u7528 <code>-lpython3.x<\/code> \u53c2\u6570\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06Python\u811a\u672c\u4e0eC\u7a0b\u5e8f\u7ed3\u5408\u4ee5\u63d0\u9ad8\u6027\u80fd\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u7ed3\u5408\u4f7f\u7528C\u8bed\u8a00\u548cPython\u53ef\u4ee5\u5145\u5206\u5229\u7528\u4e24\u8005\u7684\u4f18\u70b9\u3002C\u8bed\u8a00\u5728\u6267\u884c\u901f\u5ea6\u4e0a\u5177\u6709\u4f18\u52bf\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u6027\u80fd\u8981\u6c42\u9ad8\u7684\u4efb\u52a1\uff0c\u800cPython\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u5f00\u53d1\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002\u901a\u8fc7\u5c06C\u8bed\u8a00\u7f16\u5199\u7684\u6a21\u5757\u5d4c\u5165\u5230Python\u4e2d\uff0c\u6216\u4f7f\u7528Python\u6765\u8c03\u7528C\u7f16\u5199\u7684\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e24\u79cd\u8bed\u8a00\u7684\u65e0\u7f1d\u96c6\u6210\u3002<\/p>\n<p><strong>\u6267\u884cPython\u4ee3\u7801\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728C\u8bed\u8a00\u4e2d\u6267\u884cPython\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u6ce8\u610fPython\u73af\u5883\u7684\u8bbe\u7f6e\uff0c\u5305\u62ecPython\u7248\u672c\u548c\u5e93\u7684\u8def\u5f84\u3002\u540c\u65f6\uff0c\u8981\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff0c\u7279\u522b\u662f\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528Python\u89e3\u91ca\u5668\u65f6\u3002\u6b64\u5916\uff0c\u9519\u8bef\u5904\u7406\u4e5f\u5f88\u91cd\u8981\uff0c\u5efa\u8bae\u4f7f\u7528PyErr_Occurred()\u548c\u5176\u4ed6\u9519\u8bef\u68c0\u67e5\u673a\u5236\u6765\u6355\u6349\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"C\u8bed\u8a00\u6267\u884cPython\u4ee3\u7801\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4f7f\u7528Python\/C API\u3001\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u3001\u5d4c\u5165Python\u89e3\u91ca\u5668\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1184047,"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\/1184044"}],"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=1184044"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184044\/revisions"}],"predecessor-version":[{"id":1184051,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184044\/revisions\/1184051"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1184047"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1184044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1184044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1184044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}