【发布时间】:2022-01-04 14:19:43
【问题描述】:
我正在尝试使用 PyRun_AnyFile 将 python 嵌入到我的 c++ 项目中。
这是我的 python 脚本:
import aaa
if __name__ == "__main__":
print("Here")
这是我的 C++ 代码:
const char pFile[] = "C:\\testing.py";
FILE* fp = _Py_fopen(pFile, "r");
int ret = PyRun_AnyFile(fp, pFile);
我收到这样的错误消息:
Traceback (most recent call last):
File "C:\testing.py", line 1, in <module>
import aaa
ModuleNotFoundError: No module named 'aaa'
这是意料之中的。 我想问一下我是如何在代码中得到上述错误信息并重定向到其他错误文件的。
我试过了:
PyObject* ptype, * pvalue, * ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
Py_ssize_t string_len = 0;
std::wcout << std::wstring(PyUnicode_AsWideCharString(pvalue, &string_len)) << std::endl;
或:
PyErr_Print();
但这并不成功。
【问题讨论】: