C++ 调用Python 怎样在C++中调用Python

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++ 调用Python 怎样在C++中调用Python

DECHIN   2021-05-21 我要评论
想了解怎样在C++中调用Python的相关内容吗,DECHIN在本文为您仔细讲解C++ 调用Python的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:c++调用python,下面大家一起来学习吧。

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++的最明显优势就是在于其性能的天然优越性。但是我们不需要对哪一种编程语言有所偏倚,都有所掌握,并且能够有所互通,利用好各自的优势,才能够发挥最大的价值。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们