将python嵌入到C++中无法按预期工作
Embedding python into C++ does not work as expected
我正在将Python嵌入到C++应用程序中。
当我运行下面的C++代码并返回时间戳时,它运行得很好。
Py_Initialize();
std::string strModule = "time"; // module to be loaded
pName = PyString_FromString(strModule.c_str());
pModule = PyImport_Import(pName); // import the module
pDict = PyModule_GetDict(pModule); // get all the symbols in the module
pFunc = PyDict_GetItemString(pDict, "time"); // get the function we want to call
// Call the function and get the return in the pValue
pValue = PyObject_CallObject(pFunc, NULL);
if (pValue == NULL){
printf('Something is wrong !');
return 0;
}
printf("Return of python call : %dn", PyInt_AsLong(pValue)); // I get the correct timestamp
Py_Finalize();
现在我想得到sys.path
。但类似的代码给我带来了错误:
Py_Initialize();
std::string strModule = "sys"; // module to be loaded
pName = PyString_FromString(strModule.c_str());
pModule = PyImport_Import(pName); // import the module
pDict = PyModule_GetDict(pModule); // get all the symbols in the module
pFunc = PyDict_GetItemString(pDict, "path"); // get the function we want to call
// Call the function and get the return in the pValue
pValue = PyObject_CallObject(pFunc, NULL);
if (pValue == NULL){
printf('Something is wrong !'); // I end up here, why pValue is NULL?
return 0;
}
printf("Return of python call : %dn", PyInt_AsLong(pValue));
Py_Finalize();
我想问题是time.time()
是一个函数调用,而sys.path
是一个变量。如果是这样的话:
- 如何获得变量的结果
- 如何正确地将结果(在本例中为
list
)转换为C++中有意义的东西,例如字符串数组
如果没有,如何进行?我使用的是Python 2.7.6
谢谢。
您的问题是PyDict_GetItemString(pDict, "path")
将返回python列表,并且它是不可调用的。当你执行PyObject_CallObject(pFunc, NULL);
时,你就会执行它。这等于sys.path()
。
这应该有效:
PyObject *pName, *pModule, *pDict, *list, *pValue, *item;
int n, i;
char *name;
Py_Initialize();
std::string strModule = "sys"; // module to be loaded
pName = PyString_FromString(strModule.c_str());
pModule = PyImport_Import(pName); // import the module
pDict = PyModule_GetDict(pModule); // get all the symbols in the module
list = PyDict_GetItemString(pDict, "path"); // get python list
n = PyList_Size(list);
if (n < 0)
return -1; /* Not a list */
for (i = 0; i < n; i++) { // iterate over list
item = PyList_GetItem(list, i); /* Can't fail */
if (!PyString_Check(item)) continue; /* Skip non-string */
name = PyString_AsString(item);
std::puts(name);
}
Py_Finalize();
return 0;
此处为完整代码。
相关文章:
- pybind11:无法让 std::list 在 Python 中工作
- 为什么这在 c++ 中不起作用,但在 python 中工作
- 设备与Visa在Python中工作的通信,但在C 中不进行
- Windows API "CreateWindowEx" 在 Python (3.6.8) 64 位使用的 DLL 中工作吗?
- 使用 swig 在 Python 中分段错误(核心转储),但当我更改变量名称时它可以工作
- C 插座未接收传入的数据包,在Python中工作
- Python在调试模式下以C 工作,但在EXE文件中不工作
- 尝试将工作python程序转换为C++但不起作用
- 无法让 matplotlib 事件处理程序与 Boost.Python 一起工作
- 无法获得新的 tf。在 Tensorflow 中的 Python shell 中工作的操作
- 如何为嵌入式python解释器设置工作目录
- 为什么这个 extern "C" 函数不能使用 python ctypes 工作?
- 将python嵌入到C++中无法按预期工作
- 如何创建空的工作boost::python::dict
- 无法在opencv中从C++打开视频.它在python中工作
- Regex LookAround 在 python 中工作,但在 C++ 中不起作用
- Outlook -从显示名称获取电子邮件地址.可以在Python中工作,而不是C
- 定义一个像 Python "with" 语句一样工作的 C 宏有什么缺点?
- Python .cpp读取,追加,保存和关闭,但不工作
- c++中是否有任何数据结构或库可以像Python列表和字典一样工作?