请教:如何在VC中调用python脚本,最好给出一个简单的例子,谢谢

vallan 2003-08-22 11:54:01
请教:如何在VC中调用python脚本,最好给出一个简单的例子,谢谢
...全文
260 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbscbb 2003-09-02
  • 打赏
  • 举报
回复
下载boost,里面有支持python的 模块,cool闭了


我用mingw编译的,vc6,7就更方便了
dmchun 2003-08-28
  • 打赏
  • 举报
回复
也不知道PYTHON返回的列表在C++里变成了什么.
dmchun 2003-08-28
  • 打赏
  • 举报
回复
我想更通用的办法是C++调用一个.DLL,执行一个.PY,返回结果.但不知如何开始.
zhaoweikid 2003-08-28
  • 打赏
  • 举报
回复
/* Example of embedding Python in another program */

#include "Python.h"

void initxyzzy(void);

main(int argc, char **argv)
{
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);

/* Initialize the Python interpreter. Required. */
Py_Initialize();

/* Add a static module */
initxyzzy();

/* Define sys.argv. It is up to the application if you
want this; you can also let it undefined (since the Python
code is generally not a main program it has no business
touching sys.argv...) */
PySys_SetArgv(argc, argv);

/* Do some application specific code */
printf("Hello, brave new world\n\n");

/* Execute some Python statements (in module __main__) */
PyRun_SimpleString("import sys\n");
PyRun_SimpleString("print sys.builtin_module_names\n");
PyRun_SimpleString("print sys.modules.keys()\n");
PyRun_SimpleString("print sys.executable\n");
PyRun_SimpleString("print sys.argv\n");

/* Note that you can call any public function of the Python
interpreter here, e.g. call_object(). */

/* Some more application specific code */
printf("\nGoodbye, cruel world\n");

/* Exit, cleaning up the interpreter */
Py_Exit(0);
/*NOTREACHED*/
}

/* A static module */

/* 'self' is not used */
static PyObject *
xyzzy_foo(PyObject *self, PyObject* args)
{
return PyInt_FromLong(42L);
}

static PyMethodDef xyzzy_methods[] = {
{"foo", xyzzy_foo, METH_NOARGS,
"Return the meaning of everything."},
{NULL, NULL} /* sentinel */
};

void
initxyzzy(void)
{
PyImport_AddModule("xyzzy");
Py_InitModule("xyzzy", xyzzy_methods);
}
vallan 2003-08-24
  • 打赏
  • 举报
回复
up

37,720

社区成员

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

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