【发布时间】:2012-05-27 20:42:24
【问题描述】:
我正在尝试在我的宠物项目中嵌入一些 python。我已将问题简化为以下代码:
#include <Python.h>
#include "iostream"
int main(int argc, char *argv[])
{
Py_Initialize();
PyObject *globals = Py_BuildValue("{}");
PyObject *locals = Py_BuildValue("{}");
PyObject *string_result = PyRun_StringFlags(
"a=5\n"
"s='hello'\n"
"d=dict()\n"
,
Py_file_input, globals, locals, NULL);
if ( PyErr_Occurred() ) {PyErr_Print();PyErr_Clear();return 1;}
return 0;
}
(我知道我没有清理任何引用。这是一个示例。)
可以编译
c++ $(python-config --includes) $(python-config --libs) test.cpp -o test
如果我运行它,我会收到以下错误:
$ ./test
Traceback (most recent call last):
File "<string>", line 3, in <module>
NameError: name 'dict' is not defined
似乎内置函数没有加载。我也不能import 任何东西。我知道__import__ 不见了。如何加载缺少的模块或我缺少的任何东西?
谢谢。
【问题讨论】:
标签: python python-c-api python-embedding