作者:虛壞叔叔
博客:https://xuhss.com
早餐店不會開到晚上,想吃的人早就來了!
在test.py中添加函數:
print('test.py')
def Main():
print("Python Main")
在c++中調用這個函數
// C++調用Python函數
PyObject* Main = PyObject_GetAttrString(m, "Main");
if (Main && PyCallable_Check(Main)) {
// 函數對象和參數 返回對象
if (!PyObject_CallObject(Main, 0))
{
throw exception("PyObject_CallObject Failed");
}
}
Py_XDECREF(Main);
運行:
可以看到成功調用了。
在test.py
中添加函數TestFun
:
print('test.py')
def Main():
print("Python Main")
def TestFun(lis):
print("In Python", lis)
return [1,2,3,4,5,6,7,8,9,10]
在C++中調用
// C++調用Python函數
PyObject* Main = PyObject_GetAttrString(m, "Main");
if (Main && PyCallable_Check(Main)) {
// 函數對象和參數 返回對象
if (!PyObject_CallObject(Main, 0))
{
throw exception("PyObject_CallObject Failed");
}
}
Py_XDECREF(Main);
// c++調用Python的函數 list參數和list返回值
PyObject* TestFun = PyObject_GetAttrString(m, "TestFun");
if (TestFun && PyCallable_Check(TestFun)) {
// 參數准備 參數變量是tuple
PyObject *args = PyTuple_New(1); // 參數是元組 只有元組這個一個參數
// 傳遞的list對象
PyObject *lis = PyList_New(0);
for (int i = 0; i < 5; i++)
PyList_Append(lis, PyLong_FromLong(i + 100));
// 將list寫入元組參數列表中
PyTuple_SetItem(args, 0, lis);
// 函數對象和參數的返回值
PyObject* re = PyObject_CallObject(TestFun, args);
Py_XDECREF(args); // lis也在args中銷毀
// 處理返回值
if (re)
{
cout << "PyObject_CallObject return" << endl;
int size = PyList_Size(re);
for (int i = 0; i < size; i++)
{
PyObject *val = PyList_GetItem(re, i);
if (!val)
continue;
printf("[%d]", PyLong_AsLong(val));
}
Py_XDECREF(re);
}
}
Py_XDECREF(TestFun);
結果
可以看到調用成功了,將傳遞的列表輸出出來,返回值也可以輸出出來。
#include <iostream>
#include <Python.h>
#include <exception>
using namespace std;
int main(int argc, char*argv[])
{
cout << "C++ call Python" << endl;
// 設置Python的Home路徑
Py_SetPythonHome(L"./");
// Python初始化解釋器
Py_Initialize();
PyObject *m = NULL; // 主模塊
try
{
int re = 0;
// 執行Python腳本
re = PyRun_SimpleString("print('Hello world!')");
re = PyRun_SimpleString("print(\"__name__ = \", __name__)");
// 執行Python文件
char* filename = "test.py";
FILE * fp = fopen(filename, "r");
if (!fp)
{
throw exception("open file failed");
}
PyRun_AnyFile(fp, filename);
if (re != 0)
{
PyErr_Print();
throw exception("PyRun_AnyFile failed");
}
// 獲取主模塊
PyObject *key = PyUnicode_FromString("__main__");
m = PyImport_GetModule(key); // 不清理參數,需要手動清理
Py_XDECREF(key);
// 2-1 調用python的變量 python做配置文件
//con = {
// "width":1920,
// "heigth" : 1080,
// "title" : "C++ call Python"
//}
{
// 根據模塊和名稱獲取對象(對象可以是變量、函數和類)
PyObject* conf = PyObject_GetAttrString(m, "conf");
if (!conf) {
throw exception("conf noe find!");
}
PyObject *key = PyUnicode_FromString("width");
int width = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("height");
int height = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("title");
wchar_t title[1024] = {
0 };
int size = PyUnicode_AsWideChar(PyDict_GetItem(conf, key), title, 1023);
Py_XDECREF(key);
printf("width=%d height=%d \n", width, height);
wprintf(L"title=%s\n", title);
Py_XDECREF(conf);
}
{
// 獲取類
PyObject* TypePy = PyObject_GetAttrString(m, "TypePy");
if (!TypePy) {
throw exception("TypePy noe find!");
}
// 實例化對象 相當於調用構造函數__init__ 傳遞構造函數的參數NULL
PyObject *obj = PyObject_CallObject(TypePy, NULL);
if (!obj) {
throw exception("obj not Create!");
}
// 調用類成員函數 i(int) s(string)
PyObject *re = PyObject_CallMethod(obj, "test", "is", 2001, "c Para2");
cout << "PyObject_CallMethod return" << PyLong_AsLong(re) << endl;
Py_XDECREF(re);
// 訪問成員變量
PyObject* var = PyObject_GetAttrString(obj, "id");
cout << "TypePy.id=" << PyLong_AsLong(var) << endl;
Py_XDECREF(var);
Py_XDECREF(obj);
Py_XDECREF(TypePy);
}
{
// C++調用Python函數
PyObject* Main = PyObject_GetAttrString(m, "Main");
if (Main && PyCallable_Check(Main)) {
// 函數對象和參數 返回對象
if (!PyObject_CallObject(Main, 0))
{
throw exception("PyObject_CallObject Failed");
}
}
Py_XDECREF(Main);
// c++調用Python的函數 list參數和list返回值
PyObject* TestFun = PyObject_GetAttrString(m, "TestFun");
if (TestFun && PyCallable_Check(TestFun)) {
// 參數准備 參數變量是tuple
PyObject *args = PyTuple_New(1); // 參數是元組 只有元組這個一個參數
// 傳遞的list對象
PyObject *lis = PyList_New(0);
for (int i = 0; i < 5; i++)
PyList_Append(lis, PyLong_FromLong(i + 100));
// 將list寫入元組參數列表中
PyTuple_SetItem(args, 0, lis);
// 函數對象和參數的返回值
PyObject* re = PyObject_CallObject(TestFun, args);
Py_XDECREF(args); // lis也在args中銷毀
// 處理返回值
if (re)
{
cout << "PyObject_CallObject return" << endl;
int size = PyList_Size(re);
for (int i = 0; i < size; i++)
{
PyObject *val = PyList_GetItem(re, i);
if (!val)
continue;
printf("[%d]", PyLong_AsLong(val));
}
Py_XDECREF(re);
}
}
Py_XDECREF(TestFun);
}
Py_XDECREF(m);
// 清理python
Py_Finalize();
}
catch (const std::exception&ex)
{
if (PyErr_Occurred())
PyErr_Print();
cout << ex.what() << endl;// 清理python
Py_XDECREF(m);
Py_Finalize();
}
getchar();
return 0;
}
點贊
收藏
轉發
一波哦,博主也支持為鐵粉絲制作專屬動態壁紙哦~關注下面卡片即刻獲取更多編程知識,包括各種語言學習資料,上千套PPT模板和各種游戲源碼素材等等資料。更多內容可自行查看哦!