如何在C++中调用Python


Posted in Python onMay 21, 2021

Python的安装

为了使用Python.h这个扩展项,我们需要安装一个python*-dev而不是python*,这两者略有区别,下面的案例展示的是在Ubuntu20.04下安装python3.9-dev的方法:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ sudo apt install python3.9-dev
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列软件包是自动安装的并且现在不需要了:
  chromium-codecs-ffmpeg-extra gstreamer1.0-vaapi
  libgstreamer-plugins-bad1.0-0 linux-headers-5.8.0-43-generic
  linux-hwe-5.8-headers-5.8.0-43 linux-image-5.8.0-43-generic
  linux-modules-5.8.0-43-generic linux-modules-extra-5.8.0-43-generic
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  libexpat1-dev libpython3.9 libpython3.9-dev zlib1g-dev
下列【新】软件包将被安装:
  libexpat1-dev libpython3.9 libpython3.9-dev python3.9-dev zlib1g-dev
升级了 0 个软件包,新安装了 5 个软件包,要卸载 0 个软件包,有 30 个软件包未被升级。
需要下载 6,613 kB 的归档。
解压缩后会消耗 28.7 MB 的额外空间。
您希望继续执行吗? [Y/n] Y
获取:1 http://repo.huaweicloud.com/ubuntu focal/main amd64 libexpat1-dev amd64 2.2.9-1build1 [116 kB]
获取:2 http://repo.huaweicloud.com/ubuntu focal-updates/universe amd64 libpython3.9 amd64 3.9.0-5~20.04 [1,710 kB]
获取:3 http://repo.huaweicloud.com/ubuntu focal-updates/universe amd64 libpython3.9-dev amd64 3.9.0-5~20.04 [4,119 kB]
获取:4 http://repo.huaweicloud.com/ubuntu focal-updates/main amd64 zlib1g-dev amd64 1:1.2.11.dfsg-2ubuntu1.2 [155 kB]
获取:5 http://repo.huaweicloud.com/ubuntu focal-updates/universe amd64 python3.9-dev amd64 3.9.0-5~20.04 [512 kB]
已下载 6,613 kB,耗时 4秒 (1,594 kB/s)
正在选中未选择的软件包 libexpat1-dev:amd64。
(正在读取数据库 ... 系统当前共安装有 269544 个文件和目录。)
准备解压 .../libexpat1-dev_2.2.9-1build1_amd64.deb  ...
正在解压 libexpat1-dev:amd64 (2.2.9-1build1) ...
正在选中未选择的软件包 libpython3.9:amd64。
准备解压 .../libpython3.9_3.9.0-5~20.04_amd64.deb  ...
正在解压 libpython3.9:amd64 (3.9.0-5~20.04) ...
正在选中未选择的软件包 libpython3.9-dev:amd64。
准备解压 .../libpython3.9-dev_3.9.0-5~20.04_amd64.deb  ...
正在解压 libpython3.9-dev:amd64 (3.9.0-5~20.04) ...
正在选中未选择的软件包 zlib1g-dev:amd64。
准备解压 .../zlib1g-dev_1%3a1.2.11.dfsg-2ubuntu1.2_amd64.deb  ...
正在解压 zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu1.2) ...
正在选中未选择的软件包 python3.9-dev。
准备解压 .../python3.9-dev_3.9.0-5~20.04_amd64.deb  ...
正在解压 python3.9-dev (3.9.0-5~20.04) ...
正在设置 libpython3.9:amd64 (3.9.0-5~20.04) ...
正在设置 libexpat1-dev:amd64 (2.2.9-1build1) ...
正在设置 zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu1.2) ...
正在设置 libpython3.9-dev:amd64 (3.9.0-5~20.04) ...
正在设置 python3.9-dev (3.9.0-5~20.04) ...
正在处理用于 man-db (2.9.1-1) 的触发器 ...
正在处理用于 libc-bin (2.31-0ubuntu9.2) 的触发器 ...

安装完成后,如果在当前命令行下运行python3.9,是可以看到一个python专属的命令行界面的,可以通过exit()退出。但是我们这里侧重的是跟C++的配合工作,因此我们更加关注lib和include目录下是否有生成相关的目录,可以执行如下指令进行查看:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ ll /usr/lib/ | grep python
drwxr-xr-x  26 root root   20480 5月   7 16:27 python2.7/
drwxr-xr-x   3 root root    4096 2月  10 02:47 python3/
drwxr-xr-x  30 root root   20480 5月   7 16:30 python3.8/
drwxr-xr-x  31 root root   12288 5月  20 16:31 python3.9/

这里我们看到有一个3.9的版本,也就是我们刚才安装的版本,再看看include下的目录:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ ll /usr/include/ | grep python
drwxr-xr-x  2 root root   4096 5月   7 16:31 python3.8/
drwxr-xr-x  4 root root   4096 5月  20 16:31 python3.9/

这里我们就可以看到一些区别了,有一些版本的python不一定会有这两个目录,但是只有具备了这两个目录,才能够被C++调用。

VS Code配置

这里我们使用的IDE是VS Code,但是上述提到的几个路径,在VS Code中默认是不被包含的,因此在代码编辑的过程中在include <Python.h>这一步就会报错了。这一章节的目的主要是解决IDE中的报错问题,还不是最终运行中出现的问题,因为运行时我是通过命令行执行g++来运行的,而不是直接用IDE来跑。首先在VS Code界面上按顺序同时按住:ctrl+shift+P,在弹出的窗口中输入C/C++ Edit Configurations(JSON)查找相关JSON配置文件,在列表中点击后会自动在VS Code中打开这个配置文件:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "c++11",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

我们所需要做的工作就是,在这个includePath中把相关的路径都加上,比如我这边添加的路径是以下3个:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/python3.9/",
                "/usr/lib/python3.9/",
                "/usr/include/python3.9/cpython/"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "c++11",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

添加后,include <Python.h>就不会显示报错了。

Hello World测试

行业潜规则,我们先用C++来调用一个Python的打印函数,输出Hello World试试:

// cp.cpp
#include <Python.h>
int main(int argc, char *argv[]) {
  Py_Initialize();
  PyRun_SimpleString("print('hello world')\n");
  Py_Finalize();
  return 0;
}

这里需要注意的是一个运行方式,我们是用g++来进行编译的,但是g++默认是找不到我们刚才在IDE中所设定的几个includePath的,因此需要我们手动在编译的时候加上几个参数。这些参数其实也可以运行python3.9-config去一个一个查看,这里我们直接推荐一种可以运行成功的参数,其中最重要的是-I和-l这两个路径一定要包含:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ -o cpy cp.cpp -lm -std=c++11 -I/usr/include/python3.9/ -lpython3.9
dechin@ubuntu2004:~/projects/gitlab/dechin/$ ll
总用量 4697388
drwxrwxr-x 2 dechin dechin       4096 5月  20 17:10 ./
drwxrwxr-x 8 dechin dechin       4096 5月  19 15:32 ../
-rw-rw-r-- 1 dechin dechin        152 5月  20 17:04 cp.cpp
-rwxrwxr-x 1 dechin dechin      16776 5月  20 17:10 cpy*

运行完成后,就会在当前目录下生成一个刚才指定的名字cpy的一个可执行文件,如果是windows系统,则会生成一个cpy.exe的文件。让我们执行这个文件:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ ./cpy 
hello world

成功打印Hello World,又离成功更近了一步。

调用Python函数string.split()

在C++中如果我们想分割一个字符串,虽然说也是可以实现的,但是应该没有比Python中执行一个string.split()更加方便快捷的方案了,因此我们测试一个用C++调用Python的split函数的功能。

第一次尝试

一开始我们是写了这样一个简单的案例,用PyImport_ImportModule方法去调用pysplit这个python模块:

// cp.cpp
#include <Python.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
  Py_Initialize();
  if (!Py_IsInitialized())
	{
		cout << "Initialize failed!" << endl;
		return 0;
	}
  PyObject* pModule = NULL;
  PyObject* pFunc;
  PyRun_SimpleString("import os");
  PyRun_SimpleString("os.system('pwd')");
  pModule = PyImport_ImportModule("pysplit");
  if (pModule == NULL)
	{
		cout << "Module Not Found!" << endl;
	}
  // pFunc = PyObject_GetAttrString(pModule, "sp");
  // PyObject* args = Py_BuildValue("s", "Test String Hello Every One !");
  // PyObject* pRet = PyObject_CallObject(pFunc, args);
  string cList[10];
  // PyArg_Parse(pRet, "[items]", &cList);
  cout << "res:" << cList << endl;
  Py_Finalize();
  return 0;
}

对应的Python模块的内容为:

# pysplit.py

def sp(string):
    return string.split()

这是一个非常简单的函数,但是我们在调用的时候就直接返回了一个错误:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ -o cpy cp.cpp -lm -std=c++11 -I/usr/include/python3.9/ -lpython3.9 && ./cpy
['pysplit.py', 'cpy', 'cp.cpp']
Module Not Found!
res:0x7ffc622ae900

这个错误是说,找不到pysplit这个模块。但是我们同时借助于PyRun_SimpleString调用了Python中的os库,执行了一个查看路径和当前路径下文件的功能,我们发现这个C++文件和需要引入的pysplit.py其实是在同一个路径下的,这就很奇怪了没有导入成功。

第二次尝试

经过一番的资料查询,最后发现,即使是在相同的路径下,也需要通过Python的sys将当前目录添加到系统路径中,才能够识别到这个模块,同样也是使用PyRun_SimpleString的函数:

// cp.cpp
#include <Python.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
  Py_Initialize();
  if (!Py_IsInitialized())
	{
		cout << "Initialize failed!" << endl;
		return 0;
	}
  PyObject* pModule = NULL;
  PyObject* pFunc;
  PyRun_SimpleString("import sys");
  PyRun_SimpleString("sys.path.append('./')");
  pModule = PyImport_ImportModule("pysplit");
  if (pModule == NULL)
	{
		cout << "Module Not Found!" << endl;
	}
  pFunc = PyObject_GetAttrString(pModule, "sp");
  PyObject* args = Py_BuildValue("s", "Test String Hello Every One !");
  PyObject* pRet = PyObject_CallObject(pFunc, args);
  string cList[10];
  // PyArg_Parse(pRet, "[items]", &cList);
  cout << "res:" << cList << endl;
  Py_Finalize();
  return 0;
}

这个也可以理解,Python中的函数调用,输入参数都被打包成了一个tuple格式,比如**args,而类似**kwargs则是打包成一个字典格式,类似的功能在这篇博客中有所介绍。

第三次尝试

上面的问题,在StackOverFlow上有一个类似的情况,有一个回答解决了这个问题,解决方案是,用PyObject_CallFunctionObjArgs来替代PyObject_CallObject去实现函数调用命令,相关代码如下:

// cp.cpp
#include <Python.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
  Py_Initialize();
  if (!Py_IsInitialized())
	{
		cout << "Initialize failed!" << endl;
		return 0;
	}
  PyObject* pModule = NULL;
  PyObject* pFunc;
  PyRun_SimpleString("import sys");
  PyRun_SimpleString("sys.path.append('./')");
  pModule = PyImport_ImportModule("pysplit");
  if (pModule == NULL)
	{
		cout << "Module Not Found!" << endl;
	}
  pFunc = PyObject_GetAttrString(pModule, "sp");
  PyObject* args = Py_BuildValue("s", "Test String Hello Every One !");
  PyObject* pRet = PyObject_CallFunctionObjArgs(pFunc, args, NULL);
  int size = PyList_Size(pRet);
  cout << "List size is: " << size << endl;
  for(int i=0;i<size;i++)
  {
    PyObject* cRet = PyList_GET_ITEM(pRet, i);
    char* s;
    PyArg_Parse(cRet, "s", &s);
    cout << "The " << i << "th term is: " << s << endl;
  }
  Py_Finalize();
  return 0;
}

最后,因为从Python中获取的是一个List格式的数据,因此我们首先需要用PyList_GET_ITEM去逐项提取,然后用PyArg_Parse将提取出来的元素保存到一个C++的char字符串中,执行结果如下:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ -o cpy cp.cpp -lm -std=c++11 -I/usr/include/python3.9/ -lpython3.9 && ./cpy
List size is: 6
The 0th term is: Test
The 1th term is: String
The 2th term is: Hello
The 3th term is: Every
The 4th term is: One
The 5th term is: !

Yes!终于成功了!

总结概要

本文介绍了一个在C++内部调用Python中封装的函数或者接口的方法,从环境配置到具体示例都有讲解,并且在其中包含有不少的坑点,需要一步一步去踩。不同的编程语言具有不同的优势,Python轮子众多而语法简单,上手容易,但是性能比较首先,C++的最明显优势就是在于其性能的天然优越性。但是我们不需要对哪一种编程语言有所偏倚,都有所掌握,并且能够有所互通,利用好各自的优势,才能够发挥最大的价值。

以上就是如何在C++中调用Python的详细内容,更多关于C++ 调用Python的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python strip()函数 介绍
May 24 Python
python爬虫教程之爬取百度贴吧并下载的示例
Mar 07 Python
Python的内存泄漏及gc模块的使用分析
Jul 16 Python
Python深入学习之特殊方法与多范式
Aug 31 Python
Python 实现购物商城,含有用户入口和商家入口的示例
Sep 15 Python
如何实现删除numpy.array中的行或列
May 08 Python
对numpy.append()里的axis的用法详解
Jun 28 Python
python 去除二维数组/二维列表中的重复行方法
Jan 23 Python
python2.7的flask框架之引用js&amp;css等静态文件的实现方法
Aug 22 Python
Python 批量读取文件中指定字符的实现
Mar 06 Python
Python打印特殊符号及对应编码解析
May 07 Python
Python‘==‘ 及 ‘is‘相关原理解析
Sep 05 Python
python 定义函数 返回值只取其中一个的实现
May 21 #Python
Python+Appium实现自动抢微信红包
写好Python代码的几条重要技巧
windows安装python超详细图文教程
python如何正确使用yield
May 21 #Python
详细总结Python常见的安全问题
May 21 #Python
Pycharm 如何设置HTML文件自动补全代码或标签
You might like
坏狼php学习 计数器实例代码
2008/06/15 PHP
phpwind放自动注册方法
2006/12/02 Javascript
js 鼠标点击事件及其它捕获
2009/06/04 Javascript
JavaScript 布尔操作符解析  &amp;&amp; || !
2012/08/10 Javascript
Javascript自定义排序 node运行 实例
2013/06/05 Javascript
纯js简单日历实现代码
2013/10/05 Javascript
JS延迟加载加快页面打开速度示例代码
2013/12/30 Javascript
iscroll.js的上拉下拉刷新时无法回弹的解决方法
2016/02/18 Javascript
20分钟成功编写bootstrap响应式页面 就这么简单
2016/05/12 Javascript
JS简单获取及显示当前时间的方法
2016/08/03 Javascript
angular分页指令操作
2017/01/09 Javascript
前端把html表格生成为excel表格的实例
2017/09/19 Javascript
微信小程序实现九宫格抽奖
2020/04/15 Javascript
JS实现音乐钢琴特效
2020/01/06 Javascript
bootstrap-paginator服务器端分页使用方法详解
2020/02/13 Javascript
ES6箭头函数和扩展实例分析
2020/05/23 Javascript
python去除文件中重复的行实例
2018/06/29 Python
浅谈Python反射 &amp; 单例模式
2019/03/21 Python
Python交互式图形编程的实现
2019/07/25 Python
pyinstaller打包程序exe踩过的坑
2019/11/19 Python
Python动态导入模块和反射机制详解
2020/02/18 Python
python实现银行账户系统
2021/02/22 Python
AmazeUI 网格的实现示例
2020/08/13 HTML / CSS
Lookfantastic台湾:英国彩妆美发保养购物网
2018/03/26 全球购物
GIVENCHY纪梵希官方旗舰店:高定彩妆与贵族护肤品
2018/04/16 全球购物
Hunkemöller瑞士网上商店:欧洲最大的内衣品牌之一
2018/12/03 全球购物
Book Depository美国:全球领先的专业网上书店之一
2019/08/14 全球购物
法国最大的在线眼镜店:EasyLunettes
2019/08/26 全球购物
我的求职计划书
2014/01/10 职场文书
优秀的2014年两会精神解读
2014/03/17 职场文书
社区党的群众路线教育实践活动剖析材料
2014/10/09 职场文书
机关干部三严三实心得体会
2014/10/13 职场文书
民主评议党员工作总结
2014/10/20 职场文书
会计人员岗位职责
2015/02/03 职场文书
入党后的感想
2015/08/10 职场文书
详细了解MVC+proxy
2021/07/09 Java/Android