C语言调用python脚本
python提供了C语言的API,可以使用C语言调用python脚本。参考链接:https://docs.python.org/3/c-api/API接口函数很多,一时半会儿也看不完,先快速实现在C语言程序内执行py脚本再说吧,做个笔记。#define Py_LIMITED_API#include <stdio.h>#include <Windows.h>...
·
python提供了C语言的API,可以使用C语言调用python脚本。
参考链接:https://docs.python.org/3/c-api/
API接口函数很多,一时半会儿也看不完,先快速实现在C语言程序内执行py脚本再说吧,做个笔记。
#define Py_LIMITED_API
#include <stdio.h>
#include <Windows.h>
#include "python.h"
int py_init(void)
{
Py_SetPythonHome(L"C:\Python");
return 0;
}
int py_exec(wchar_t *script)
{
wchar_t *argv[2];
argv[0] = L"python";
argv[1] = script;
return Py_Main(2, argv);
}
int main(void)
{
int ret;
wchar_t *script = L"X:\Path\demo.py";
ret = py_init();
ret = py_exec(script);
return ret;
}
解释一下:
#define Py_LIMITED_API 表示只使用python3.dll提供的API函数,兼容性较强,否则会调用python36.dll。
Py_SetPythonHome(home) 设置python的根目录,否则会报错,找不到
Py_Main(argc, argv) 调用python的主函数,没有参数的话会进入python命令行,给2个参数就可以执行指定脚本。
更多推荐
已为社区贡献1条内容
所有评论(0)