Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to embed python script in C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to embed python script in C++". In daily operation, I believe many people have doubts about how to embed python script in C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to embed python script in C++". Next, please follow the editor to study!

1. Install Python

1.1 install Python on Windows:

Go directly to the relevant website to download an installation package python-2.5.1.msi (* version) and double-click the installation file.

1.2 install Python on linux:

Develop on linux, download the original code, install through the original code need to perform configure, make, make install several steps, I download and install is python-2.4.tgz.

2. Preliminary work of development

In the win32 environment, Lib and Include; after adding the Python installation to the VC Directories are specified by gcc in the linux environment Lib and Include.

3. Write Python scripts

The Python function is not difficult to write, either in a text editor or in IDLE (officially provided by Python). Here is a simple python script with two numbers added:

Def PyAdd (x, y) nSum = x + y print "Sum =", nSum return nSum

4. Call embedded Python script in C++

4.1 introduction of header files

Win32/linux needs to introduce Python header files

Ifdef WIN32 # include "Python.h" # else # include "/ usr/src/Python-2.4/Include/Python.h" # endif

4.2 in the process of embedding C++ in Python to make up for some shortcomings of C++ itself, C++ needs to initialize before calling Python, and close Python before the end of the program

Initialize Python Py_Initialize ()

Close Python Py_Finalize ()

4.3 the linux environment requires code to introduce the python library after Py_Initialize

# ifndef WIN32 PyRun_SimpleString ("import sys"); PyRun_SimpleString ("sys.path.append ('. /')); PyRun_SimpleString (" import os "); PyRun_SimpleString (" import string "); # endif

The specific library to be introduced is determined according to the needs.

C++ embedded Python preparation work done after C++ programming in order to call Python function scalability, a CallPyFunction function is specially designed to call Python function, Python script file name, function name and parameters are all passed in by CallPyFunction. The following code is the core code of CallPyFunction, and the code does not contain error handling.

Int CallPyFunction (const char * pszModuleName, const char * pModulFuncName, const char * pParam [], const int nCount) {PyObject * pName = NULL; PyObject * pModule = NULL; PyObject * pDict = NULL; PyObject * pFunc = NULL; PyObject * pParams = NULL; PyObject * pCurrParam = NULL; int i = 0; pName = PyString_FromString (pszModuleName); pModule = PyImport_Import (pName); pDict = PyModule_GetDict (pModule); pFunc = PyDict_GetItemString (pDict, pModulFuncName); pParams = PyTuple_New (PyTuple_New) I = 0; while (I < nCount) {pCurrParam = PyString_FromString (pParam [I]); PyTuple_SetItem (pParams, I, pCurrParam); iTunes;} PyObject * pFtp= PyObject_CallObject (pFunc, pParams); return true;} this ends the study of "how to embed a python script in C++". I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report