您的位置:首页 > 编程语言 > C语言/C++

【VS2013】C语言调用Python脚本环境搭建、函数调用和调用demo.py脚本an

2017-04-14 19:29 801 查看
1、环境配置:

1)安装VS2013(注意语言包一定要正确安装)

如果提示--MS VS 检测到已安装的语言资源版本不匹配。则如下处理:

建立vs_langpack.exe的快捷方式,然后修改路径,后面加上“空格 /Uninstall”

例如:(“E:\Downloads\vs_langpack.exe /Uninstall”)

然后修复安装即可

2)安装PTVS 2.2 VS 2013.msi工具,默认安装就可以了

3)在Python的安装文件中配置

配置系统环境Path:E:\Python\Python27

然后在Python的安装目录中的的include文件夹中(E:\Python\Python27\include)配置

【1】修改Python.h文件

在Python.h文件的 

#include "patchlevel.h"
#include "pyconfig.h"
#include "pymacconfig.h"上面添加下面两个其一,推荐第一个(亲自试过)

#define Py_NO_ENABLE_SHARED

#define MS_NO_COREDLL

【2】修改 pyconfig.h
修改
#ifdef _DEBUG
# define Py_DEBUG
#endif

#ifdef _DEBUG
//# define Py_DEBUG
#endif

 
修改
# ifdef _DEBUG
# pragma comment(lib,"python24_d.lib")
# else
# pragma comment(lib,"python24.lib")
# endif

# ifdef _DEBUG
# pragma comment(lib,"python24.lib")
# else
# pragma comment(lib,"python24.lib")
# endif

 
【3】修改object.h
修改
#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
#define Py_TRACE_REFS
#endif

#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
// #define Py_TRACE_REFS
#endif

4)在VS2013中创建一个空的Win32 控制台应用程序

在项目中右键弹出的对话框中选择属性

1)修改C/C++下的常规



2)修改链接器下的常规



3)修改链接器下的输入添加python27.lib;(这个没有,是手动写的)



到此环境的配置全部结束。

2、在c程序中直接调用Python代码:

#include <Python.h>

int main(int argc, char *argv[])

{
Py_Initialize();
PyRun_SimpleString("print 'Hello Python!'\n");
Py_Finalize();
getchar();
return 0;

}



3、在c程序中直接调用Python脚本

注意事项:Demo.cpp(调用程序)一定要与test.py(被调用文件)放在一个目录下



test.py:

print 'hello python test version'





[b]Demo.cpp:
[/b]#include <Python.h>

int main(int argc, char *argv[])

{
Py_Initialize();
char szFile[] = "test.py";

FILE* fp = fopen(szFile, "r");
if (fp && PyRun_SimpleString("execfile('test.py')") != 0)
{
fclose(fp);

printf("PyRun_SimpleFile(%s)failed!", szFile);

return -1;
}
Py_Finalize();

//让命令行停住
getchar();
return 0;

}



4、在c程序中直接调用Python脚本中的函数

注意事项:Demo.cpp(调用程序)一定要与test.py(被调用文件)放在一个目录下

math_test.py

def add_func(a,b):

    return a+b
def sub_func(a,b):

    return (a-b)




math_test.py

#include <stdio.h> #include <stdlib.h> #include <string.h>
#include <Python.h>
int main(int argc, char** argv)
{
int arg0 = 30;
int arg1 = 50;
Py_Initialize();
if (!Py_IsInitialized())
return -1;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject *pModule;
PyObject *pFunction;
PyObject *pArgs;
PyObject *pRetValue;
pModule = PyImport_ImportModule("math_test");
if (!pModule){
printf("import python failed!!\n");
return -1;
}
pFunction = PyObject_GetAttrString(pModule, "add_func");
if (!pFunction){
printf("get python function failed!!!\n");
return -1;
}
pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", arg0));
PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", arg1));
pRetValue = PyObject_CallObject(pFunction, pArgs);
printf("%d + %d = %ld\n", arg0, arg1, PyInt_AsLong(pRetValue));
Py_DECREF(pModule);
Py_DECREF(pFunction);
Py_DECREF(pArgs);
Py_DECREF(pRetValue);
Py_Finalize();

getchar();
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: