C++嵌入Python,引入包时出现问题

rena521 2014-04-30 09:34:21
我在研究复杂网络时,想通过将Python嵌入C++,然后使用Python的igraph接口进行展示。
现在有一个问题,就是我的Python程序中需要引入igraph,完整程序如下:
#Filename showgml.py
from igraph import *
def show():
g = Graph.Read_GML("d:\\football.gml")
plot(g)


然后在C++中嵌入Python:
// mytestpython.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "python.h"
int _tmain(int argc, _TCHAR* argv[])
{
Py_Initialize();
if(!Py_IsInitialized())
{
return -1;
}
PyRun_SimpleString("execfile('C:\\Python27\\showgml.py')");
Py_Finalize();
return 0;
}

运行后报错
请问是什么原因呢?应如何解决?谢谢~
...全文
299 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rena521 2014-05-01
  • 打赏
  • 举报
回复
引用 3 楼 turingo 的回复:
打印一下sys.path,看看igraph模块是否包含在搜索路径中。 [quote=引用 楼主 rena521 的回复:] 我在研究复杂网络时,想通过将Python嵌入C++,然后使用Python的igraph接口进行展示。 现在有一个问题,就是我的Python程序中需要引入igraph,完整程序如下:
#Filename showgml.py
from igraph import *
def show():
	g = Graph.Read_GML("d:\\football.gml")
	plot(g)
然后在C++中嵌入Python:
// mytestpython.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "python.h"
int _tmain(int argc, _TCHAR* argv[])
{
	Py_Initialize();
	if(!Py_IsInitialized())
	{
		return -1;
	}
	PyRun_SimpleString("execfile('C:\\Python27\\showgml.py')");
	Py_Finalize();
	return 0;
}
运行后报错 请问是什么原因呢?应如何解决?谢谢~
[/quote] 路径是包含整个PYTHON文件夹的,难道是igraph里还包含了什么dll文件吗?
图灵狗 2014-05-01
  • 打赏
  • 举报
回复
很有可能的啊。
引用 4 楼 rena521 的回复:
[quote=引用 3 楼 turingo 的回复:] 打印一下sys.path,看看igraph模块是否包含在搜索路径中。 [quote=引用 楼主 rena521 的回复:] 我在研究复杂网络时,想通过将Python嵌入C++,然后使用Python的igraph接口进行展示。 现在有一个问题,就是我的Python程序中需要引入igraph,完整程序如下:
#Filename showgml.py
from igraph import *
def show():
	g = Graph.Read_GML("d:\\football.gml")
	plot(g)
然后在C++中嵌入Python:
// mytestpython.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "python.h"
int _tmain(int argc, _TCHAR* argv[])
{
	Py_Initialize();
	if(!Py_IsInitialized())
	{
		return -1;
	}
	PyRun_SimpleString("execfile('C:\\Python27\\showgml.py')");
	Py_Finalize();
	return 0;
}
运行后报错 请问是什么原因呢?应如何解决?谢谢~
[/quote] 路径是包含整个PYTHON文件夹的,难道是igraph里还包含了什么dll文件吗?[/quote]
图灵狗 2014-04-30
  • 打赏
  • 举报
回复
打印一下sys.path,看看igraph模块是否包含在搜索路径中。
引用 楼主 rena521 的回复:
我在研究复杂网络时,想通过将Python嵌入C++,然后使用Python的igraph接口进行展示。 现在有一个问题,就是我的Python程序中需要引入igraph,完整程序如下:
#Filename showgml.py
from igraph import *
def show():
	g = Graph.Read_GML("d:\\football.gml")
	plot(g)
然后在C++中嵌入Python:
// mytestpython.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "python.h"
int _tmain(int argc, _TCHAR* argv[])
{
	Py_Initialize();
	if(!Py_IsInitialized())
	{
		return -1;
	}
	PyRun_SimpleString("execfile('C:\\Python27\\showgml.py')");
	Py_Finalize();
	return 0;
}
运行后报错 请问是什么原因呢?应如何解决?谢谢~
rena521 2014-04-30
  • 打赏
  • 举报
回复
python文件可以单独正常运行,而且如果把C++嵌入的python文件换成另一个不需要引入包的文件如:
print 'Hello, world!!'
也可正常运行。
火头军 2014-04-30
  • 打赏
  • 举报
回复
你的python程序不嵌入的时候可以正常运行吗?如果不能应该是py那边的问题

65,207

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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