请问各路专家,我在c++调用Python的时候成功调用文件但是调用不到函数该怎么解决

请问各路专家,我在c++调用Python的时候成功调用文件但是调用不到函数该怎么解决

问题描述:

附上代码
c++代码


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

int main()
{
    //Py_SetPythonHome(L"C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python37_64/python.exe");

    Py_Initialize();
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('c:/programing')");

    PyObject* pModule = NULL;
    PyObject* pFunc = NULL;

    pModule = PyImport_ImportModule("callFunction");
    if (pModule == NULL)
    {
        std::cout << "The file does not found" << std::endl;
        return false;
    }
    
    pFunc = PyObject_GetAttrString(pModule, "calltest");
    if (pFunc == NULL)
    {
        std::cout << "The function does not exist" << std::endl;
        return false;
    }

}

Python代码


import sys

def calltest():
    return 255

if __name__=='__main__':
    print(calltest())

解决了,path里面要直接包括py文件