【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;
}
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;
}
相关文章推荐
- py-faster-rcnn(running the demo): ubuntu14.04+caffe+cuda7.5+cudnn5.1.3+python2.7环境搭建记录
- Python基于VS2013 开发环境搭建 Hello World 10分钟搞定
- Python调用C++函数(SWIG,VS2013使用numpy.i完成Numpy与C++数组转换)
- VS2013中Python学习笔记[环境搭建]
- VS2013中Python学习环境搭建
- [VS2013]C++中调用Python脚本提示 error LNK2001: 无法解析的外部符号__imp_PyString_FromString等错误的解决方案
- S32v234 VS2013 demo环境搭建
- 在Mac终端中,用Python脚本A 调用Python脚本B(函数)运行环境小结
- CEF:JavaScript 调用 C++ 函数 Demo(VS2013)
- Qt通过python脚本调用Tensorflow环境搭建
- CEF:C++ 调用 JavaScript 函数 Demo(VS2013)
- win10下caffe环境搭建: win10 + vs2013 + caffe + CUDA 7.5 + cudnn v4 + Anaconda2 (python 2.7) 目前未使用GPU
- C语言调用Python脚本中的函数
- 基于VS2013 X64的boost.python环境搭建
- cocos2d-x-2.2.2 + Visual studio 2013 + python2.7.6开发环境搭建
- c语言调用Python脚本-2
- [wp8游戏] cocos2d-x v2.2 + VS2013 环境搭建
- [Cocos2d-x 3.0rc1] Cocos2d-x Windows+VS2013环境搭建
- C调用Python脚本文件里的函数
- apache+php搭建一个简单的web环境【php调用脚本】