c++调用python详解(多输入多输出)

//初始化python解释器Py_Initialize();if ( !Py_IsInitialized() ) { return false;}//开始使用PyRun_SimpleString 运行python语句, 设置被调用的python脚本的全路径PyRun_SimpleString("import sys");PyRun_SimpleString("sys.path.a
摘要由CSDN通过智能技术生成

Linux 下 c++调用python程序示例,python函数的输入输出皆为数组形式

环境 Ubuntu python2.7  c++11

自己整理的代码,希望大家也能参考使用到


c++程序如下 mycpp.cpp:

#include <Python.h>
#include <iostream>
#include <vector>

int main(){
	PyObject* pyName;
	PyObject* pyModule;
	PyObject* pyDict;
	PyObject* pyFunc;
	PyObject* pyArgs;

	//初始化python解释器
	Py_Initialize();
	if (!Py_IsInitialized()) {
		return false;
	}

	//开始使用PyRun_SimpleString 运行python语句, 设置被调用的python脚本的全路径
	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('./')");

	//导入python脚本文件testpy.py,此处的脚本文件名不需要加扩展名
	pyName = PyString_FromString("mypy");

	//加载python模型,作用类似python的 import
	pyModule = PyImport_Import(pyName); if (!pyModule) {
		printf("can't find mypy.py\n");
		return false;
	}

	//取得模块接口字典信息
	pyDict = PyModule_GetDict(pyModule);
	if (!pyDict)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值