C++ 调用 Python 模块

// C++ call Python module 
// author: huihut
// repo: https://gist.github.com/huihut/b4597d097123a8c8388c71b3f0ff21e5


#include <iostream>
#include <Python.h>

// C++ call Python module
bool CppCallPython()
{
    // Python initialize
    Py_Initialize();
    if (!Py_IsInitialized())
    {
        std::cout << "Python initialization failed!\n";
        return false;
    }

    // If my MyPython.py file is in "/Users/xx/code", set the working path to "/Users/xx/code"
    std::string path = "/Users/xx/code";
    PySys_SetPath(&path[0u]);

    // Import MyPython.py module
    PyObject* pModule = PyImport_ImportModule("MyPython");
    if (!pModule)
    {
        std::cout <<"Cannot open Python file!\n";
        return false;
    }

    // Get the HelloPython() function in the module
    PyObject* pFunhello = PyObject_GetAttrString(pModule, "HelloPython");
    if (!pFunhello)
    {
        std::cout << "Failed to get this function!";
        return false;
    }

    // Call HelloPython()
    PyObject_CallFunction(pFunhello, NULL);

    // Finalize
    Py_Finalize();

    return true;
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读