{"id":1010097,"date":"2024-12-27T11:17:57","date_gmt":"2024-12-27T03:17:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1010097.html"},"modified":"2024-12-27T11:18:00","modified_gmt":"2024-12-27T03:18:00","slug":"c%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e5%b5%8c%e5%85%a5python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1010097.html","title":{"rendered":"c\u8bed\u8a00\u5982\u4f55\u5d4c\u5165python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084752\/6f90184c-4938-457c-be17-903b90c4769f.webp\" alt=\"c\u8bed\u8a00\u5982\u4f55\u5d4c\u5165python\" \/><\/p>\n<p><p> <strong>\u5728C\u8bed\u8a00\u4e2d\u5d4c\u5165Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684C API\u3001\u5d4c\u5165Python\u89e3\u91ca\u5668\u6216\u4f7f\u7528\u6269\u5c55\u6a21\u5757\u8fdb\u884c\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528Python\u7684C API\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8C\u7a0b\u5e8f\u8c03\u7528Python\u4ee3\u7801\u5e76\u83b7\u53d6\u7ed3\u679c\u3002\u5d4c\u5165Python\u89e3\u91ca\u5668\u5219\u662f\u5c06Python\u89e3\u91ca\u5668\u96c6\u6210\u5230C\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u80fd\u591f\u76f4\u63a5\u6267\u884cPython\u811a\u672c\u3002\u6269\u5c55\u6a21\u5757\u5219\u662f\u901a\u8fc7\u7f16\u5199C\u6269\u5c55\u6765\u6269\u5c55Python\u7684\u529f\u80fd\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u4f7f\u7528Python\u7684C API\u6765\u5d4c\u5165Python\u4ee3\u7801\u5230C\u7a0b\u5e8f\u4e2d\u3002<\/p>\n<\/p>\n<p><p>Python\u7684C API\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u548c\u5b8f\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728C\u7a0b\u5e8f\u4e2d\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3001\u6267\u884cPython\u4ee3\u7801\u3001\u8c03\u7528Python\u51fd\u6570\u4ee5\u53ca\u7ba1\u7406Python\u5bf9\u8c61\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Python\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728C\u7a0b\u5e8f\u4e2d\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u8ba1\u7b97\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728C\u8bed\u8a00\u4e2d\u5d4c\u5165Python\uff0c\u5305\u62ec\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3001\u6267\u884cPython\u4ee3\u7801\u3001\u8c03\u7528Python\u51fd\u6570\u548c\u7ba1\u7406Python\u5bf9\u8c61\u7b49\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521d\u59cb\u5316Python\u89e3\u91ca\u5668<\/p>\n<\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\u5d4c\u5165Python\u7684\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570<code>Py_Initialize()<\/code>\u7528\u4e8e\u521d\u59cb\u5316\u89e3\u91ca\u5668\u3002\u5728\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5305\u542bPython\u7684\u5934\u6587\u4ef6<code>Python.h<\/code>\uff0c\u8fd9\u4e2a\u5934\u6587\u4ef6\u63d0\u4f9b\u4e86\u4f7f\u7528Python\u7684C API\u6240\u9700\u7684\u6240\u6709\u58f0\u660e\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\u884cPython\u4ee3\u7801<\/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\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5305\u542b\u4e86<code>Python.h<\/code>\u5934\u6587\u4ef6\uff0c\u7136\u540e\u5728<code>main<\/code>\u51fd\u6570\u4e2d\u8c03\u7528<code>Py_Initialize()<\/code>\u51fd\u6570\u6765\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3002\u6700\u540e\uff0c\u6211\u4eec\u5728\u7a0b\u5e8f\u7ed3\u675f\u4e4b\u524d\u8c03\u7528<code>Py_Finalize()<\/code>\u51fd\u6570\u6765\u7ed3\u675fPython\u89e3\u91ca\u5668\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>Py_Initialize()<\/code>\u548c<code>Py_Finalize()<\/code>\u51fd\u6570\u5fc5\u987b\u6210\u5bf9\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6267\u884cPython\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u5728\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7C API\u4e2d\u7684<code>PyRun_SimpleString()<\/code>\u51fd\u6570\u6765\u6267\u884cPython\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>    Py_Initialize();<\/p>\n<p>    \/\/ \u6267\u884cPython\u4ee3\u7801<\/p>\n<p>    PyRun_SimpleString(&quot;print(&#39;Hello from Python!&#39;)&quot;);<\/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\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>PyRun_SimpleString()<\/code>\u51fd\u6570\u6267\u884c\u4e86\u4e00\u6bb5\u7b80\u5355\u7684Python\u4ee3\u7801\uff0c\u8be5\u4ee3\u7801\u5c06\u8f93\u51fa\u4e00\u884c\u6587\u672c\u201cHello from Python!\u201d\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8c03\u7528Python\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6267\u884c\u7b80\u5355\u7684Python\u4ee3\u7801\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7C API\u8c03\u7528Python\u51fd\u6570\u3002\u4e3a\u4e86\u8c03\u7528Python\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u5148\u5bfc\u5165\u5305\u542b\u8be5\u51fd\u6570\u7684Python\u6a21\u5757\uff0c\u7136\u540e\u83b7\u53d6\u8be5\u51fd\u6570\u7684\u5bf9\u8c61\uff0c\u6700\u540e\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>    Py_Initialize();<\/p>\n<p>    \/\/ \u5bfc\u5165Python\u6a21\u5757<\/p>\n<p>    PyObject *pName = PyUnicode_DecodeFSDefault(&quot;mymodule&quot;);<\/p>\n<p>    PyObject *<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/52627.html\" target=\"_blank\">PMO<\/a>dule = PyImport_Import(pName);<\/p>\n<p>    Py_DECREF(pName);<\/p>\n<p>    if (pModule != NULL) {<\/p>\n<p>        \/\/ \u83b7\u53d6Python\u51fd\u6570<\/p>\n<p>        PyObject *pFunc = PyObject_GetAttrString(pModule, &quot;myfunction&quot;);<\/p>\n<p>        \/\/ \u68c0\u67e5\u51fd\u6570\u662f\u5426\u53ef\u8c03\u7528<\/p>\n<p>        if (PyCallable_Check(pFunc)) {<\/p>\n<p>            \/\/ \u8c03\u7528Python\u51fd\u6570<\/p>\n<p>            PyObject *pValue = PyObject_CallObject(pFunc, NULL);<\/p>\n<p>            Py_XDECREF(pValue);<\/p>\n<p>        } else {<\/p>\n<p>            PyErr_Print();<\/p>\n<p>        }<\/p>\n<p>        Py_XDECREF(pFunc);<\/p>\n<p>        Py_DECREF(pModule);<\/p>\n<p>    } else {<\/p>\n<p>        PyErr_Print();<\/p>\n<p>    }<\/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\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7<code>PyImport_Import()<\/code>\u51fd\u6570\u5bfc\u5165\u4e00\u4e2a\u540d\u4e3a<code>mymodule<\/code>\u7684Python\u6a21\u5757\uff0c\u7136\u540e\u901a\u8fc7<code>PyObject_GetAttrString()<\/code>\u51fd\u6570\u83b7\u53d6\u6a21\u5757\u4e2d\u7684<code>myfunction<\/code>\u51fd\u6570\u5bf9\u8c61\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>PyCallable_Check()<\/code>\u68c0\u67e5\u8be5\u5bf9\u8c61\u662f\u5426\u4e3a\u53ef\u8c03\u7528\u51fd\u6570\u3002\u6700\u540e\uff0c\u6211\u4eec\u901a\u8fc7<code>PyObject_CallObject()<\/code>\u51fd\u6570\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ba1\u7406Python\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\u5d4c\u5165Python\u65f6\uff0c\u6211\u4eec\u9700\u8981\u7ba1\u7406Python\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002Python\u7684C API\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u7ba1\u7406Python\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u4f8b\u5982<code>Py_INCREF()<\/code>\u548c<code>Py_DECREF()<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u6bcf\u4e2aPython\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570\uff0c\u5f53\u5f15\u7528\u8ba1\u6570\u4e3a\u96f6\u65f6\uff0cPython\u4f1a\u81ea\u52a8\u91ca\u653e\u8be5\u5bf9\u8c61\u5360\u7528\u7684\u5185\u5b58\u3002\u6211\u4eec\u9700\u8981\u901a\u8fc7<code>Py_INCREF()<\/code>\u589e\u52a0\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u5e76\u5728\u4e0d\u518d\u9700\u8981\u8be5\u5bf9\u8c61\u65f6\u901a\u8fc7<code>Py_DECREF()<\/code>\u51cf\u5c11\u5f15\u7528\u8ba1\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>    Py_Initialize();<\/p>\n<p>    PyObject *pList = PyList_New(0);  \/\/ \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684Python\u5217\u8868<\/p>\n<p>    Py_INCREF(pList);  \/\/ \u589e\u52a0\u5f15\u7528\u8ba1\u6570<\/p>\n<p>    \/\/ \u4f7f\u7528pList...<\/p>\n<p>    Py_DECREF(pList);  \/\/ \u51cf\u5c11\u5f15\u7528\u8ba1\u6570<\/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\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u7684Python\u5217\u8868<code>pList<\/code>\uff0c\u5e76\u901a\u8fc7<code>Py_INCREF()<\/code>\u589e\u52a0\u4e86\u5b83\u7684\u5f15\u7528\u8ba1\u6570\u3002\u5f53\u6211\u4eec\u4e0d\u518d\u9700\u8981\u8be5\u5bf9\u8c61\u65f6\uff0c\u901a\u8fc7<code>Py_DECREF()<\/code>\u51cf\u5c11\u5f15\u7528\u8ba1\u6570\uff0c\u786e\u4fdd\u5185\u5b58\u80fd\u591f\u88ab\u6b63\u786e\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406Python\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u5728\u5d4c\u5165Python\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230Python\u4ee3\u7801\u629b\u51fa\u7684\u5f02\u5e38\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u6211\u4eec\u9700\u8981\u5728\u8c03\u7528Python C API\u51fd\u6570\u540e\u68c0\u67e5\u662f\u5426\u6709\u5f02\u5e38\u53d1\u751f\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>    Py_Initialize();<\/p>\n<p>    \/\/ \u6267\u884c\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684Python\u4ee3\u7801<\/p>\n<p>    if (PyRun_SimpleString(&quot;raise Exception(&#39;Test Exception&#39;)&quot;) != 0) {<\/p>\n<p>        \/\/ \u6253\u5370\u5f02\u5e38\u4fe1\u606f<\/p>\n<p>        PyErr_Print();<\/p>\n<p>    }<\/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\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6267\u884c\u4e86\u4e00\u6bb5Python\u4ee3\u7801\uff0c\u8be5\u4ee3\u7801\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002\u6211\u4eec\u901a\u8fc7\u68c0\u67e5<code>PyRun_SimpleString()<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6765\u5224\u65ad\u662f\u5426\u6709\u5f02\u5e38\u53d1\u751f\uff0c\u5e76\u901a\u8fc7<code>PyErr_Print()<\/code>\u51fd\u6570\u6253\u5370\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528Python\u6269\u5c55\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528Python\u6269\u5c55\u6a21\u5757\u6765\u6269\u5c55Python\u7684\u529f\u80fd\u3002Python\u6269\u5c55\u6a21\u5757\u901a\u5e38\u662f\u7528C\u6216C++\u7f16\u5199\u7684\uff0c\u5e76\u53ef\u4ee5\u901a\u8fc7C API\u4e0ePython\u8fdb\u884c\u4ea4\u4e92\u3002\u6211\u4eec\u53ef\u4ee5\u5728C\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u6269\u5c55\u6a21\u5757\u6765\u8c03\u7528Python\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u6269\u5c55\u6a21\u5757\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u6a21\u5757\u5df2\u7ecf\u5b89\u88c5\u5728Python\u73af\u5883\u4e2d\uff0c\u5e76\u5728C\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u5bfc\u5165\u6269\u5c55\u6a21\u5757\u7684\u65b9\u6cd5\u4e0e\u5bfc\u5165\u666e\u901aPython\u6a21\u5757\u7684\u65b9\u6cd5\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;Python.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    \/\/ \u5bfc\u5165\u6269\u5c55\u6a21\u5757<\/p>\n<p>    PyObject *pName = PyUnicode_DecodeFSDefault(&quot;myextension&quot;);<\/p>\n<p>    PyObject *pModule = PyImport_Import(pName);<\/p>\n<p>    Py_DECREF(pName);<\/p>\n<p>    if (pModule != NULL) {<\/p>\n<p>        \/\/ \u4f7f\u7528\u6269\u5c55\u6a21\u5757\u7684\u529f\u80fd<\/p>\n<p>        \/\/ ...<\/p>\n<p>        Py_DECREF(pModule);<\/p>\n<p>    } else {<\/p>\n<p>        PyErr_Print();<\/p>\n<p>    }<\/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\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86\u4e00\u4e2a\u540d\u4e3a<code>myextension<\/code>\u7684\u6269\u5c55\u6a21\u5757\uff0c\u5e76\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u4e2d\u63d0\u4f9b\u7684\u529f\u80fd\u6765\u5b9e\u73b0\u6211\u4eec\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5728C\u8bed\u8a00\u4e2d\u5d4c\u5165Python\u7684\u57fa\u672c\u65b9\u6cd5\u548c\u6b65\u9aa4\uff0c\u5305\u62ec\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\u3001\u6267\u884cPython\u4ee3\u7801\u3001\u8c03\u7528Python\u51fd\u6570\u3001\u7ba1\u7406Python\u5bf9\u8c61\u3001\u5904\u7406Python\u5f02\u5e38\u4ee5\u53ca\u4f7f\u7528Python\u6269\u5c55\u6a21\u5757\u7b49\u3002\u4f7f\u7528Python\u7684C API\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728C\u7a0b\u5e8f\u4e2d\u5145\u5206\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u540c\u65f6\u4fdd\u6301C\u8bed\u8a00\u7684\u9ad8\u6548\u6027\u548c\u7075\u6d3b\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5d4c\u5165Python\uff0c\u4ece\u800c\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u8ba1\u7b97\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>C\u8bed\u8a00\u5d4c\u5165Python\u7684\u4f18\u70b9\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>C\u8bed\u8a00\u5d4c\u5165Python\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6267\u884c\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5927\u91cf\u8ba1\u7b97\u7684\u4efb\u52a1\u4e2d\u3002\u901a\u8fc7\u5c06\u6027\u80fd\u654f\u611f\u7684\u4ee3\u7801\u7528C\u8bed\u8a00\u7f16\u5199\u5e76\u5d4c\u5165Python\uff0c\u53ef\u4ee5\u5728\u4fdd\u7559Python\u7b80\u6d01\u6027\u7684\u540c\u65f6\uff0c\u83b7\u5f97\u66f4\u5feb\u7684\u8fd0\u884c\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u8fd9\u79cd\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u5229\u7528C\u8bed\u8a00\u7684\u4e30\u5bcc\u5e93\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u529f\u80fd\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5728C\u8bed\u8a00\u4e2d\u5982\u4f55\u8c03\u7528Python\u4ee3\u7801\uff1f<\/strong><br \/>\u8981\u5728C\u8bed\u8a00\u4e2d\u8c03\u7528Python\u4ee3\u7801\uff0c\u9996\u5148\u9700\u8981\u5305\u542bPython\u7684\u5934\u6587\u4ef6\uff0c\u5e76\u94fe\u63a5Python\u5e93\u3002\u4f7f\u7528<code>Py_Initialize()<\/code>\u521d\u59cb\u5316Python\u89e3\u91ca\u5668\uff0c\u63a5\u7740\u53ef\u4ee5\u4f7f\u7528<code>PyRun_SimpleString()<\/code>\u7b49\u51fd\u6570\u6765\u6267\u884cPython\u4ee3\u7801\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>Py_Finalize()<\/code>\u6765\u7ed3\u675fPython\u89e3\u91ca\u5668\u7684\u4f7f\u7528\u3002\u8fd9\u4e9b\u6b65\u9aa4\u4f7f\u5f97C\u7a0b\u5e8f\u80fd\u591f\u76f4\u63a5\u6267\u884cPython\u811a\u672c\u6216\u8c03\u7528Python\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406C\u4e0ePython\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\uff1f<\/strong><br \/>\u5728C\u8bed\u8a00\u548cPython\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\/C API\u63d0\u4f9b\u7684\u7ed3\u6784\u4f53\u548c\u51fd\u6570\u6765\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7<code>Py_BuildValue()<\/code>\u5c06C\u6570\u636e\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>PyArg_Parse()<\/code>\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aC\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u4e24\u79cd\u8bed\u8a00\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u4e92\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u7075\u6d3b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728C\u8bed\u8a00\u4e2d\u5d4c\u5165Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684C API\u3001\u5d4c\u5165Python\u89e3\u91ca\u5668\u6216\u4f7f\u7528\u6269\u5c55\u6a21\u5757\u8fdb\u884c\u3002 \u5176 [&hellip;]","protected":false},"author":3,"featured_media":1010109,"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\/1010097"}],"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=1010097"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010097\/revisions"}],"predecessor-version":[{"id":1010111,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010097\/revisions\/1010111"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1010109"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1010097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1010097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1010097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}