一 、配置方法
1、下载安装vs2015与python39
2、配置属性
(1)、设置头文件和库目录
2、添加附加依赖项
(2)、该项为再python安装目录下的libs文件中,复制python39后重命名为python39_d
二、接口函数
1、相关函数
Py_Initialize();//初始化Python环境
Py_Finalize(); //关闭Python环境
Py_IsInitialized(): //判断Python解释器是否初始化成功
PyErr_Print(); //错误信息显示
PrErr_Clear(); //清除错误信息缓存
PyRun_SimpleString(); //执行简单的Python语句
//Python提供的执行对象及其相关函数
PtObiect*
PyImpot_Import();
PyCallable_Check();
PyTuple_New
PyLong_AsLong();
PyUnicode_FromString();
PyObject_GetAttrString();
PyObject_CallObject();
PyyTuple_Setltem();
PyArg_Parse();
2、实例程序
(1)两个python程序:
a)无参数无返回值:
def hh():
print("sdfjksf")
b)有参数有返回值
def add_num(a,b):
return a+b
c)无参数,返回值为字典
def dict_get():
one_dict = {}
list1 = ["a", "b", "c", "d", "e", "f", "g" ,"h", "i", "j"]
for j in list1:
for i in range(10):
one_dict[j] = i
return one_dict
(2)c++调用python程序
#include <iostream>
#include <Python.h>
using namespace std;
//通过接口函数调用无参数无返回值的python函数
void test01()
{
Py_Initialize();//使用python之前,要调用Py_Initialize();这个函数进行初始化
if (!Py_IsInitialized()) //判断是否初始化成功
{
printf("初始化失败!");
return;
}
PyRun_SimpleString("import sys"); //导入python的sys系统库
//输入文件地址
string abc = "F:/InvokePython/python"; //python程序文件存储的地址
std::string importDir = "sys.path.append('" + abc + "')\n"; //
PyRun_SimpleString(importDir.c_str());
PyObject * pModule = NULL;//声明变量
PyObject * pFunc = NULL;// 声明变量
pModule = PyImport_ImportModule("fun");//这里是要调用的文件名hello.py,纯名字,不包含.py
if (pModule == NULL)
{
cout << "没找到文件!" << endl;
return;
}
pFunc = PyObject_GetAttrString(pModule, "hh");//输入调用的函数名
if (!pFunc || !PyCallable_Check(pFunc))
{
cout << "获取函数失败!" << endl;
return;
}
PyObject_CallObject(pFunc, NULL);//调用无参数无返回值的python函数
Py_Finalize(); // 与初始化对应
}
//通过接口函数调用有参数有返回值的python函数
void test02()
{
Py_Initialize();//使用python之前,要调用Py_Initialize();这个函数进行初始化
if (!Py_IsInitialized()) //判断是否初始化成功
{
printf("初始化失败!");
return;
}
PyRun_SimpleString("import sys"); //导入python的sys系统库
//输入文件地址
string abc = "F:/InvokePython/python"; //python程序文件存储的地址
std::string importDir = "sys.path.append('" + abc + "')\n";
PyRun_SimpleString(importDir.c_str());
PyObject * pModule = NULL;//声明变量
PyObject * pFunc = NULL;// 声明变量
pModule = PyImport_ImportModule("inout");//这里是要调用的文件名hello.py,纯名字,不包含.py
if (pModule == NULL)
{
cout << "没找到" << endl;
}
pFunc = PyObject_GetAttrString(pModule, "add_num");//这里是要调用的函数名
PyObject* args = Py_BuildValue("(ii)", 2, 3);//给python函数参数赋值
PyObject* pResult = PyObject_CallObject(pFunc, args); //执行函数,获得返回值
if (!pResult)
{
cout << "获取结果失败!" << endl;
}
int res;
PyArg_Parse(pResult, "i", &res); //类型转换,res为返回值
cout << "Result: " << res << endl;
}
//通过接口函数调用无参数有返回值的python函数(返回字典)
void test03()
{
Py_Initialize();//使用python之前,要调用Py_Initialize();这个函数进行初始化
if (!Py_IsInitialized()) //判断是否初始化成功
{
printf("初始化失败!");
return;
}
PyRun_SimpleString("import sys"); //导入python的sys系统库
//输入文件地址
string abc = "F:/InvokePython/python"; //python程序文件存储的地址
std::string importDir = "sys.path.append('" + abc + "')\n";
PyRun_SimpleString(importDir.c_str());
PyObject * pModule = NULL;//声明变量
PyObject * pFunc = NULL;// 声明变量
pModule = PyImport_ImportModule("22");//这里是要调用的文件名hello.py,纯名字,不包含.py
if (pModule == NULL)
{
cout << "没找到" << endl;
}
pFunc = PyObject_GetAttrString(pModule, "dict_get");//这里是要调用的函数名
//获取字典类型返回值
PyObject* pResult = PyObject_CallObject(pFunc, NULL); //执行函数,获得返回值
if (!pResult)
{
cout << "获取结果失败!" << endl;
}
int size = PyDict_Size(pResult);
cout << "返回字典的大小为:" << size << endl;
PyObject *pNewa = PyDict_GetItemString(pResult, "b");
int res;
PyArg_Parse(pNewa, "i", &res); //类型转换,res为返回值
cout << "Result: " << res << endl;
Py_Finalize();
}
int main()
{
test01();//通过接口函数调用无参数无返回值的python函数
test02();//通过接口函数调用有参数有返回值的python函数
test03();//通过接口函数调用无参数有返回值的python函数(返回字典)
system("pause");
return 0;
}