C++调用Python找不到import模块的函数

fxbszj 2017-11-11 05:47:07

Py_Initialize();
if (!Py_IsInitialized()) {
PyErr_Print(); //打印错误信息
return;
}

string chdir_cmd = string("sys.path.append(\"") + path + "\")";

PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.argv = ['']");
PyRun_SimpleString(chdir_cmd.c_str());

// 加载模块
pModuleCalc = PyImport_ImportModule("calcu_embedding");
if (!pModuleCalc) // 加载模块失败
{
PyErr_Print();
return;
}
pModuleLocate = PyImport_ImportModule("locate_face");
if (!pModuleLocate) {
PyErr_Print();
return ;
}

// 加载函数
pInit = PyObject_GetAttrString(pModuleCalc, "init_gs");
pCalc = PyObject_GetAttrString(pModuleCalc, "calcu_embedding");
if (!pInit || !PyCallable_Check(pInit) || !pCalc || !PyCallable_Check(pCalc)) // 验证是否加载成功
{
PyErr_Print();
return;
}
// 加载函数
pLocate = PyObject_GetAttrString(pModuleLocate, "locate_face");
if (!pLocate || !PyCallable_Check(pLocate)) {
PyErr_Print();
return ;
}

initArgs = PyTuple_New(1);
ret = PyTuple_SetItem(initArgs, 0, Py_BuildValue("s", modelpath);
pInitRet = PyObject_CallObject(pInit, NULL);


上面是c++调用Python的代码, 在上面Path目录下有calcu_embedding.py和它需要Import的facenet.py ,calcu_embedding中的 init_gs函数把modelpath作为参数传给facenet的load_model,python运行没有问题,C++调用就会“AttributeError: module 'facenet' has no attribute 'load_model'”


import facenet
#def init_gs(modelPath):
def init_gs():
#initiate session and graph
start = time.clock()
g = tf.Graph()
g.as_default()
sess = tf.Session()
sess.as_default()

# Load the model
#model = modelPath + '/models/20170512-110547/20170512-110547.pb'
model = 'E:/code/python/facenet/src/models/20170512-110547/20170512-110547.pb'
facenet.load_model(model)


#下面是facenet的
def load_model(model):
# Check if the model is a model directory (containing a metagraph and a checkpoint file)
# or if it is a protobuf file with a frozen graph
model_exp = os.path.expanduser(model)
if (os.path.isfile(model_exp)):
print('Model filename: %s' % model_exp)
with gfile.FastGFile(model_exp,'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
else:
print('Model directory: %s' % model_exp)
meta_file, ckpt_file = get_model_filenames(model_exp)

print('Metagraph file: %s' % meta_file)
print('Checkpoint file: %s' % ckpt_file)

saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file))
saver.restore(tf.get_default_session(), os.path.join(model_exp, ckpt_file))
...全文
779 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
威尔金斯bhf 2017-12-20
  • 打赏
  • 举报
回复
楼主,我也遇到一样的问题了,请问解决了吗
fxbszj 2017-11-13
  • 打赏
  • 举报
回复
算了,散分散分
fxbszj 2017-11-12
  • 打赏
  • 举报
回复
引用 3 楼 ckc 的回复:
没玩过c调用python,胡乱说下我的看法 报错出在module 'facenet' has no attribute 'load_model'而不是出在import facenet上 说明import的时候是没错的 那是不是python调用时的sys.path和c调用时的sys.path不一样? 实际上存在多个不同版本的facenet? 如果不是这个问题那就是c调用模块里的函数有什么特殊之处? 如果是这样的话就写两个简单的模块来测试看看
sys.path自然是不一样的,我刚试过,就是我C调用python的时候把path设为Python调用的时候一模一样,仍然是有这个问题的
ckc 2017-11-12
  • 打赏
  • 举报
回复
没玩过c调用python,胡乱说下我的看法 报错出在module 'facenet' has no attribute 'load_model'而不是出在import facenet上 说明import的时候是没错的 那是不是python调用时的sys.path和c调用时的sys.path不一样? 实际上存在多个不同版本的facenet? 如果不是这个问题那就是c调用模块里的函数有什么特殊之处? 如果是这样的话就写两个简单的模块来测试看看
sprawling 2017-11-12
  • 打赏
  • 举报
回复
其实如果这样最好贴个示例工程出来,这样会比较好查找原因的.
fxbszj 2017-11-11
  • 打赏
  • 举报
回复
引用 1 楼 sprawling 的回复:
感觉有点像import顺序的问题.
直接python跑是没问题的,应该不存在import顺序的问题吧
sprawling 2017-11-11
  • 打赏
  • 举报
回复
感觉有点像import顺序的问题.

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧