C++呼叫Python 3.7.0
阿新 • • 發佈:2018-11-06
開發環境:Visual Studio 2015 + Python 3.7.0
一、在C++程式碼中寫Python指令碼
在C++中含有呼叫Python指令碼的API函式,在C++程式碼中嵌入Python指令碼程式碼,是通過PyRun_SimpleString()函式實現。它允許將Python指令碼程式碼寫成字串,作為PyRun_SimpleString()函式引數,從而實現Python指令碼的解析與執行,這種方法適用於Python指令碼簡短的情形,其主要流程如下圖所示:
下面通過一則例項演示C++呼叫Python的這種方式。
(1)C++程式碼:
#include <iostream>
#include <Python.h>
using namespace std;
int main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
//The Py_SetProgramName() function should be called before Py_Initialize()
//to inform the interpreter about paths to Python run-time libraries
Py_SetProgramName(program); //optional but recommended
// the Python interpreter is initialized with Py_Initialize()
Py_Initialize();
//pass a string containing Python statements to PyRun_SimpleString()
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
//the Py_FinalizeEx() call shuts the interpreter down
if (Py_FinalizeEx() < 0) {
exit(120);
}
PyMem_RawFree(program);
//system("pause");
//return 0;
return getchar();
}
(2)程式執行結果:
二、C++呼叫本地Python指令碼檔案
在很多情形,Python指令碼並不是那麼簡短,在C++程式碼中直接嵌入Python指令碼程式碼雖然也可以達到需求,但這樣的程式碼往往不易閱讀與維護。因此,C++ 中的Python API允許通過載入本地Python指令碼檔案的方式呼叫Python。下面通過一則例項演示C++呼叫Python的這種方式
(1)C++程式碼:
#include <iostream>
#include <Python.h>
using namespace std;
/*
This code loads a Python script using argv[1], and calls the function named in argv[2].
Its integer arguments are the other values of the argv array.
*/
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;
if (argc < 3)
{
fprintf(stderr, "Usage: call pythonfile funcname [args]\n");
return 1;
}
// initializing the interpreter
Py_Initialize();
pName = PyUnicode_DecodeFSDefault(argv[1]);
/* Error checking of pName left out */
// the script is loaded using PyImport_Import()
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL)
{
// Once the script is loaded, the name we’re looking for is retrieved using PyObject_GetAttrString()
pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc))
{
pArgs = PyTuple_New(argc - 3);
for (int i = 0; i < argc - 3; ++i)
{
pValue = PyLong_FromLong(atoi(argv[i + 3]));
if (!pValue)
{
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, i, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL)
{
printf("Result of call: %ld\n", PyLong_AsLong(pValue));
Py_DECREF(pValue);
}
else
{
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr, "Call failed\n");
return 1;
}
}
else
{
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else
{
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
if (Py_FinalizeEx() < 0)
{
return 120;
}
return getchar();
}
(2)Python指令碼檔案MyMultiply.py程式碼:
def multiply(a,b):
print("Will compute", a, "times", b)
c = 0
for i in range(0, a):
c = c + b
return c
(3)VS命令列引數:
MyMultiply multiply 5 3
(4)程式執行結果: