我有一个带有 pybind11 嵌入式 python 解释器的 c++ 程序,执行以下 python 文件,它直接打印到std::cout
# test.py
print("text")
Run Code Online (Sandbox Code Playgroud)
执行该文件的c++程序:
#include <pybind11/embed.h>
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{};
py::eval_file("test.py");
}
Run Code Online (Sandbox Code Playgroud)
我发现的其他解决方案需要修改 python 文件 - 如何将 python sys.stdout 作为 std::string 重定向到 c++,而不仅使用 print() 语句修改 python 代码?