简体   繁体   中英

How to call a python function defined in another file from c++?

I need your help!

How can i call a python function in c++ based on the following .py sources:

DefineEasyCalls.py

def myfun():
    return 100 

Testfile.py

from DefineEasyCalls import *
fun = myfun()

I would like to use the Testfile.py as Inputfile in my c++ programm. There, i would like to call myfun() and do something with it. Do you have an ideas how to accomplish this? Maybe i import both files in c++ and check if in Testfile.py is a method called myfun and than call the corresponding function from the testfile.py file, but what if there are function inputs for myfun in Testfile.py? In that case, this strategy would not work for me.

If i define myfun directly (code in Testfile.py = DefineEasyCalls.py) in Testfile.py, than things work. But i want to use Testfile.py as input file an not DefineEasyCalls.py! Furthermore, i would also like to give myfun in Testfile.py some inputarguments which i also want to use later on in c++. Have somebody an idea how to do this?

Here is my c++ code:

#include <Python.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int callmyfun();

int main()
{
    int a = callmyfun();
    int b = 1;
    cout << a+b << endl; 
    cin.get();
    return 0;
}


int callmyfun()
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pValue;

Py_Initialize();

PyObject* sysPath = PySys_GetObject("path");
PyList_Append(sysPath, PyBytes_FromString("C:/ .. path .../x64/Release"));

// Load the module
pName = PyUnicode_DecodeFSDefault("Testfile");
pModule = PyImport_Import(pName);
Py_DECREF(pName);

cout << pName << endl;

if (pModule != NULL)
{
    cout << "module found\n";
    pDict = PyModule_GetDict(pModule);
    pFunc = PyObject_GetAttrString(pModule, "myfun");

    if (pFunc != NULL) {
        pValue = PyObject_CallObject(pFunc, NULL);
        printf("Result of call: %ld\n", PyLong_AsLong(pValue));
        Py_DECREF(pValue);
        long long L = PyLong_AsLongLong(pValue);
        return L;
    }
    else {
        std::cout << "Couldn't find func\n";
        return 0;
    }
}
else
{
    cout << "module not found!\n";
    return 0;
}

     Py_Finalize();
}

I use MSVC2015 and python 3.6. Thank you for your help!

Found a solution that works for me! :)

  • Create a c++ class which contains the desired function
  • Create a python extension module (i used swig)
  • Call the function via the python extension module in Testfile.py

Another way would maybe to put DefineEasyCalls.py into the module directory loading path and than import this file in Testfile.py but this didn't worked for me. Here i could not find the path in which the c api searches for python modules (such as numpy). Maybe somebody knows how to get thinks work this way??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM