如何从c ++调用另一个文件中定义的python函数?

时间:2023-01-01 15:59:39

I need your help!

我需要你的帮助!

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

如何基于以下.py源在c ++中调用python函数:

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.

我想在我的c ++程序中使用Testfile.py作为Inputfile。在那里,我想打电话给myfun()并用它做点什么。你有想法如何实现这一目标吗?也许我用c ++导入这两个文件并检查Testfile.py中是否是一个名为myfun的方法,而不是从testfile.py文件调用相应的函数,但是如果在Testfile.py中有myfun的函数输入怎么办?在这种情况下,这种策略对我不起作用。

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?

如果我在Testfile.py中直接定义myfun(Testfile.py = DefineEasyCalls.py中的代码),那么事情就好了。但我想使用Testfile.py作为输入文件而不是DefineEasyCalls.py!此外,我还想在Testfile.py中给myfun一些输入参数,我也想稍后在c ++中使用它。有人知道如何做到这一点?

Here is my c++ code:

这是我的c ++代码:

#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!

我使用MSVC2015和python 3.6。谢谢您的帮助!

1 个解决方案

#1


0  

Found a solution that works for me! :)

找到适合我的解决方案! :)

  • Create a c++ class which contains the desired function
  • 创建一个包含所需函数的c ++类

  • Create a python extension module (i used swig)
  • 创建一个python扩展模块(我使用了swig)

  • Call the function via the python extension module in Testfile.py
  • 通过Testfile.py中的python扩展模块调用该函数

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??

另一种方法是将DefineEasyCalls.py放入模块目录加载路径,然后在Testfile.py中导入此文件,但这对我没有用。在这里,我找不到c api搜索python模块的路径(例如numpy)。也许有人知道如何以这种方式让思考工作?

#1


0  

Found a solution that works for me! :)

找到适合我的解决方案! :)

  • Create a c++ class which contains the desired function
  • 创建一个包含所需函数的c ++类

  • Create a python extension module (i used swig)
  • 创建一个python扩展模块(我使用了swig)

  • Call the function via the python extension module in Testfile.py
  • 通过Testfile.py中的python扩展模块调用该函数

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??

另一种方法是将DefineEasyCalls.py放入模块目录加载路径,然后在Testfile.py中导入此文件,但这对我没有用。在这里,我找不到c api搜索python模块的路径(例如numpy)。也许有人知道如何以这种方式让思考工作?