Python PyImport\u ImportModule始终返回NULL,即使设置了系统路径
我试图在Ubuntu17.10中编写一个简单的C/C++程序,用它调用python函数。但是,它总是抱怨无法使用Python PyImport\u ImportModule始终返回NULL,即使设置了系统路径,python,c++,embedding,Python,C++,Embedding,我试图在Ubuntu17.10中编写一个简单的C/C++程序,用它调用python函数。但是,它总是抱怨无法使用PyImport\u ImportModule导入模块 我已将Python.h包含在cpp文件中,并按如下方式编译它: g++ -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/ -g -o main main.cpp -I/usr/include/python3.6 -lpython3.6 我尝试过以下方法,但没有一种有效: 使用P
PyImport\u ImportModule
导入模块
我已将Python.h
包含在cpp文件中,并按如下方式编译它:
g++ -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/ -g -o main main.cpp -I/usr/include/python3.6 -lpython3.6
我尝试过以下方法,但没有一种有效:
PySys\u GetObject
和PyList\u Insert
将当前路径添加到path
(根据此)#include <iostream>
#include <python3.6/Python.h>
using namespace std;
int main()
{
Py_Initialize();
PyObject* sysPath = PySys_GetObject("path");
PyObject *path = PyBytes_FromString("/tmp");
int result = PyList_Append(sysPath, path);
PyObject* module = PyImport_ImportModule("hello");
if (0 != module)
{
PyObject* helloFunc = PyObject_GetAttrString(module, "hello");
if (helloFunc)
{
PyEval_CallObject(helloFunc, 0);
}
else
{
fprintf(stderr, "hello function is NULL");
}
}
else
{
fprintf(stderr, "hello module is NULL -- somehow");
}
Py_Finalize();
return 0;
}
有人能告诉我为什么在我的简单程序中PyImport\u ImportModule
总是返回NULL吗
非常感谢 此程序正在运行的帐户是否具有读取
/tmp/hello.py
的权限?是的,我已将其设置为777,但它不起作用。我面临相同的问题,您是否能够修复它?
class hello(object):
def __init__(self):
print("Hello world: init")
def hello(self):
print("Hello world: hello")