将 python 文件导入 c++ 程序 - ModuleNotFoundError

问题描述 投票:0回答:0

关注这个thread

假设我在目录 prog 中有以下文件:

main.cpp(位于目录prog)

int main(int argc, char *argv[]){


Py_Initialize();
PyObject* myModuleString = PyUnicode_DecodeFSDefault((char*)"multiset_bell_numbers.py");
PyObject *pModule =  PyImport_Import(myModuleString);
if (pModule == nullptr) {
    PyErr_Print();
    throw std::runtime_error("pModule is empty");
}

return 0;
}

multiset_bell_numbers.py(位于目录prog)

from sympy.utilities.iterables import multiset_partitions



def calc_partitions(arr):
    res = list(multiset_partitions(arr))
    res.sort(key=lambda part:len(part))
    return res

但是还是报错:

ModuleNotFoundError:没有名为“multiset_bell_numbers”的模块

我也试过从字符串的后缀中删除“.py” 我该怎么办?

python c++ python-c-api python-embedding
© www.soinside.com 2019 - 2024. All rights reserved.