An in-depth analysis of C++ calling Python modules
Python provides a C++ library that allows developers to easily call Python modules from C++ programs. Next, through this article, I will introduce you to the relevant knowledge of C++ calling Python modules. Friends who need it can refer to it
Generally, those who have developed games know that Lua and C++ can be well combined, learn from each other's strengths, and use Lua scripts Used like a dynamic link library, it makes good use of the flexibility of script development. As a popular general-purpose scripting language, Python can also do it. In a C++ application, we can use a set of plug-ins to implement some functions with a unified interface. Generally, plug-ins are implemented using dynamic link libraries. If plug-ins change frequently, we can use Python instead of dynamic link libraries. Plug-ins (which can be called dynamic link libraries in text form) make it easy to rewrite script code according to changes in requirements, instead of having to recompile and link binary dynamic link libraries. Flexibility is greatly improved.
As a glue language, Python can easily call C, C++ and other languages, and can also call Python modules through other languages.
Python provides a C++ library that allows developers to easily call Python modules from C++ programs.
For specific documentation, please refer to the official guide:
Embedding Python in Another Application
Calling method
1 Link to Python calling library
The Python installation directory already contains header files (include directory) and library files (python27.lib under Windows).
You need to link to this library before using it.
2 Directly call the Python statement
<code class="language-cpp hljs ">#include "python/Python.h" int main() { Py_Initialize(); ## 初始化 PyRun_SimpleString("print 'hello'"); Py_Finalize(); ## 释放资源 } </code>
3 Load the Python module and call the function
~/test directory contains test .py :
<code class="language-python hljs ">def test_add(a, b): print 'add ', a, ' and ', b return a+b</code>
The test_add function can be called through the following code:
<code class="language-cpp hljs ">#include "python/Python.h" #include <iostream> using namespace std; int main() { Py_Initialize(); // 初始化 // 将Python工作路径切换到待调用模块所在目录,一定要保证路径名的正确性 string path = "~/test"; string chdir_cmd = string("sys.path.append(\"") + path + "\")"; const char* cstr_cmd = chdir_cmd.c_str(); PyRun_SimpleString("import sys"); PyRun_SimpleString(cstr_cmd); // 加载模块 PyObject* moduleName = PyString_FromString("test"); //模块名,不是文件名 PyObject* pModule = PyImport_Import(moduleName); if (!pModule) // 加载模块失败 { cout << "[ERROR] Python get module failed." << endl; return 0; } cout << "[INFO] Python get module succeed." << endl; // 加载函数 PyObject* pv = PyObject_GetAttrString(pModule, "test_add"); if (!pv || !PyCallable_Check(pv)) // 验证是否加载成功 { cout << "[ERROR] Can't find funftion (test_add)" << endl; return 0; } cout << "[INFO] Get function (test_add) succeed." << endl; // 设置参数 PyObject* args = PyTuple_New(2); // 2个参数 PyObject* arg1 = PyInt_FromLong(4); // 参数一设为4 PyObject* arg2 = PyInt_FromLong(3); // 参数二设为3 PyTuple_SetItem(args, 0, arg1); PyTuple_SetItem(args, 1, arg2); // 调用函数 PyObject* pRet = PyObject_CallObject(pv, args); // 获取参数 if (pRet) // 验证是否调用成功 { long result = PyInt_AsLong(pRet); cout << "result:" << result; } Py_Finalize(); ## 释放资源 return 0; } </iostream></code>
Parameter passing
1 C++ passes parameters to Python
The parameters of Python are actually tuples, so passing parameters is actually constructing a suitable tuple.
There are two commonly used methods:
Use PyTuple_New to create a tuple, and PyTuple_SetItem to set the tuple value
<code class="language-cpp hljs ">PyObject* args = PyTuple_New(3); PyObject* arg1 = Py_BuildValue("i", 100); // 整数参数 PyObject* arg2 = Py_BuildValue("f", 3.14); // 浮点数参数 PyObject* arg3 = Py_BuildValue("s", "hello"); // 字符串参数 PyTuple_SetItem(args, 0, arg1); PyTuple_SetItem(args, 1, arg2); PyTuple_SetItem(args, 2, arg3);</code>
directly Use Py_BuildValue to construct a tuple
<code class="language-cpp hljs ">PyObject* args = Py_BuildValue("ifs", 100, 3.14, "hello"); PyObject* args = Py_BuildValue("()"); // 无参函数</code>
For format strings such as i, s, f, please refer to the format string
2 Convert Python Return Value
What you get when calling Python are PyObject objects, so you need to use some functions in the library provided by Python to convert the return value to C++, such as PyInt_AsLong, PyFloat_AsDouble, PyString_AsString, etc.
You can also use the PyArg_ParseTuple function to parse the return value as a tuple.
PyArg_Parse is also a conversion function that is very convenient to use.
PyArg_ParseTuple and PyArg_Parse both use format strings
Notes
You need to switch the Python working directory to the location of the module The path is loaded according to the module name instead of the file name. Module loading or function loading needs to be verified successfully, otherwise it may cause stack errors and cause the program to crash. Py_DECREF(PyObject*) needs to be used to dereference the object (for Python garbage collection)
The above is the relevant knowledge of C++ calling Python modules introduced by the editor. I hope it will be helpful to everyone!
For more in-depth analysis of C++ calling Python modules, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
