首页 > 解决方案 > 从 C++ PyBind11 中的另一个 Python 文件导入 Python 文件(模块)

问题描述

好的。因此,由于我在 Github 上的问题没有得到正确回答,并且有人建议我去一个合适的论坛,所以我来了。我正在尝试从另一个嵌入 PyBind11 的 Python 文件中导入 Python 文件。问题是这样的:

我有两个文件: mts.py

def add(a, b):
    return a + b

main.py

print('Hello, world!')

我有我的 C++ 代码:

py::initialize_interpreter(); // I am purposely not using eval_file because in the future I might not be using files, but strings instead.
py::exec(readFile("mts.py"));
py::exec(readFile("main.py"));
py::finalize_interpreter();

我的问题是,如何在“main.py”中导入“mts.py”?即我如何在'main.py'中调用'add'?

标签: pythonc++moduleembedpybind11

解决方案


推荐阅读