{"id":213,"date":"2021-12-02T00:06:14","date_gmt":"2021-12-01T16:06:14","guid":{"rendered":"https:\/\/www.346pro.club\/?p=213"},"modified":"2021-12-02T00:06:15","modified_gmt":"2021-12-01T16:06:15","slug":"%e5%9c%a8c%e4%b8%ad%e8%b0%83%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/www.346pro.club\/?p=213","title":{"rendered":"\u5728C++\u4e2d\u8c03\u7528Python"},"content":{"rendered":"\n<p>\u56e0\u4e3a\u5de5\u4f5c\u539f\u56e0\uff0c\u7b97\u6cd5\u90e8\u5206\u662fpython\u5199\u7684\uff0c\u4e4b\u524d\u4e00\u76f4\u7528rpc\u7684\u65b9\u6cd5\u6765\u8c03\u7528python\u51fd\u6570\uff0c\u6211\u611f\u89c9\u8fd9\u79cd\u65b9\u6cd5\u6709\u4e00\u70b9\u4e0d\u591f\u7b80\u6d01\uff08\uff1f\uff09\uff0c\u4e8e\u662f\u6253\u7b97\u5f15\u5165Python.h\u7684\u5e93\u6765\u505a\u8fd9\u4e00\u5de5\u4f5c\uff0c\u6240\u4ee5\u5c31\u6709\u4e86\u665a\u4e0a\u7684\u5b9e\u64cd\u3002<\/p>\n\n\n\n<p>\u4e0d\u591a\u8bf4\uff0c\u4e0a\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;iostream>\n#include &lt;Python.h>\n\nint main()\n{\n    \/\/_putenv(\"PYTHONHOME=D:\/Users\/hoshinokun\/miniconda3\/envs\/python_3_7\");\n\n    Py_Initialize();\n    if (!Py_IsInitialized()) {\n        std::cout &lt;&lt; \"python init fail\" &lt;&lt; std::endl;\n        return 0;\n    }\n\n    PyRun_SimpleString(\"import sys\");\n    PyRun_SimpleString(\"sys.path.append('.\/')\");\n\n    PyObject* pModule = PyImport_ImportModule(\"test2\");\n    if (pModule == nullptr)\n    {\n        PyErr_Print();\n        std::exit(1);\n    }\n\n    PyObject* pFunc = PyObject_GetAttrString(pModule, \"press\");\n\n    PyObject* pReturn = PyEval_CallObject(pFunc, NULL);\n\n    bool nResult;\n\n    PyArg_Parse(pReturn, \"i\", &amp;nResult);\n\n    Py_Finalize();\n    std::cout &lt;&lt; \"return result is \" &lt;&lt; nResult &lt;&lt; std::endl;\n    std::cout &lt;&lt; \"Hello World!\\n\";\n}<\/pre>\n\n\n\n<p>\u5176\u4e2d\u6709\u51e0\u4e2a\u6ce8\u610f\u7684\u70b9\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5982\u679c\u4f60\u7684python\u5e93\u4e0d\u5728PATH\u91cc\u9762\uff0c\u4f60\u9700\u8981\u7528c\u7684\u51fd\u6570_putenv\u6765\u5c06\u4f60\u7684python\u8def\u5f84\u52a0\u5230PYTHONHOME\u7684\u91cc\u9762\uff0c\u5426\u5219\u4f60\u7684\u51fd\u6570\u521d\u59cb\u5316Py_Initialize\u65f6\u5019\u4f1a\u62a5\u9519\u76f4\u63a5\u5f39\u51fa\u3002<\/li><li>\u5728vs\u8c03\u8bd5\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u8def\u5f84\u662fvs\u6e90\u7801\u7684\u8def\u5f84\u800c\u4e0d\u662f\u5e94\u7528\u7a0b\u5e8f\u7684\u8def\u5f84\uff0c\u4f60\u9700\u8981\u628atest2.py\u6587\u4ef6\u548c\u4f60\u7684cpp\u4ee3\u7801\u653e\u5728\u4e00\u8d77<\/li><li>PyImport_ImportModule\u7684\u65f6\u5019\uff0c\u4e00\u5f00\u59cb\u6211\u4e00\u76f4\u62a5\u9519\uff0cpModule\u4e00\u76f4\u4e3aNULL\uff0c\u6240\u4ee5\u8fd9\u4e2a\u65f6\u5019\u5f15\u5165\u4e86PyErr_Print\u51fd\u6570\uff0c\u5176\u53ef\u4ee5\u8f93\u51fa\u4e00\u4e2apython\u6807\u51c6\u9519\u8bef\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u91cc\u9762\u770b\u5230\u4f60\u5177\u4f53\u662f\u4ec0\u4e48\u6837\u7684\u9519\u8bef<\/li><li>PyErr_Print\u51fd\u6570\u8f93\u51fa\u4e86\u4e00\u4e2acv2\u5f15\u7528\u9519\u8bef\uff0c\u67e5\u4e86\u4e00\u4e0b\u8d44\u6599\u53d1\u73b0\uff0cpython\u7684\u5e93\u662f\u6ca1\u6709_d\u7684\u8c03\u8bd5\u5e93\u7684\uff0c\u5728\u8fd0\u884c\u8c03\u8bd5\u7248\u672c\u7684\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u4f1a\u56e0\u4e3a\u627e\u4e0d\u5230\u8c03\u8bd5\u5e93\u800c\u76f4\u63a5\u62a5\u9519\u5f39\u51fa\u627e\u4e0d\u5230\u5e93\uff0c\u6240\u4ee5\u9700\u8981\u5728Release\u73af\u5883\u4e0b\u9762\u53bb\u8c03\u8bd5\u7a0b\u5e8f<\/li><\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56e0\u4e3a\u5de5\u4f5c\u539f\u56e0\uff0c\u7b97\u6cd5\u90e8\u5206\u662fpython\u5199\u7684\uff0c\u4e4b\u524d\u4e00\u76f4&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.346pro.club\/?p=213\">Continue reading<span class=\"screen-reader-text\">\u5728C++\u4e2d\u8c03\u7528Python<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-technology","entry"],"_links":{"self":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=213"}],"version-history":[{"count":0,"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}