C ++ calling Python summary

  Recent test data set through open source model, the official label is not suitable for some finishing processing for the C ++ format, so be prepared to achieve by mixing programming and Python. Online on how to achieve a lot of information, here is a summary of the information under used and problems encountered.

1, project configuration

  python must be packed, said that this is mainly down vs configurations.
  C ++ function call Python script
  reference this blog, C ++ is mainly used to call Python under Python installation path include, libs, the directory-related dll. Do not need to copy the files on the settings, you need to speak dll path to the environment variable "Path", add the include file path to the VS project property "contains the directory" libs folder path will be added to the VS project properties "library catalog "and in the project properties - Add relevant files lib libs folder> accessories dependencies in -> linker.

2, C ++ calls the Python process

  • We first need to include the header file Python.h
  • Initialize the Python environment Py_Initialize()
  • Add the module path (or script path)PyRun_SimpleString()
  • Import modulePyImport_ImportModule()
  • Import functionPyObject_GetAttrString()
  • Use functionPyObject_CallObject()
  • Getting ResultsPyArg_Parse()
  • The end of the release Py_DECREF(),Py_Finalize()

3, initialization problem

Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'

  1, initialization call Py_Initialize()on the error, the issue is on the Internet to check python version there is no problem, the path is set right. Finally, C ++ calling Python Py_Initialize failed to solve the problem, namely adding Py_SetPythonHome (L "<Python path>"), should be noted that the author is added Scripts folder path, and I added to the Python layer of sentence can be, related dll Python also in the root directory.

  2, call PyImport_ImportModule load python always returns NULL. Reference c ++ python module is loaded, but PyImport_ImportModule old returns NULL , mainly to ensure that "sys.path.append ()" is written in the python script path, and the script can be run without error.

4, input parameters

  For the use of C ++ embedded Python in C ++ part Kisser Leon's thematic blog, on the input parameters, relatively simple method is to use Py_BuildValue, in embedding the entry python c ++ 2 has use cases.

    command The actual input
Py_BuildValue("") None
Py_BuildValue(“i”, 123) 123
Py_BuildValue(“iii”, 123, 456, 789) (123, 456, 789)
Py_BuildValue(“s”, “hello”) ‘hello’
Py_BuildValue(“ss”, “hello”, “world”) (‘hello’, ‘world’)
Py_BuildValue(“s#”, “hello”, 4) ‘hell’
Py_BuildValue("()") ()
Py_BuildValue("(i)", 123) (123,)
Py_BuildValue("(ii)", 123, 456) (123, 456)
Py_BuildValue("(i,i)", 123, 456) (123, 456)
Py_BuildValue("[i,i]", 123, 456) [123, 456]
Py_BuildValue("{s:i,s:i}", “abc”, 123, “def”, 456) {‘abc’: 123, ‘def’: 456}
Py_BuildValue("((ii)(ii)) (ii)", 1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6))

5, the interface view

  Python version upgrade its C API interface is also likely to change, such as the one mentioned in PyString_AsString Python2 not find in Python3 the
  C API PyString_AsString in Python3.0 replaced by what?
  The answer PyByte_AsString, PyUnicode_AsString are not found in Python3.5 in, but we're on by looking at the Python dll is found, we can also see what the interface in their own dll, dll methods can be used to view third-party tools, but also you can use windows built-in commands directly:

dumpbin /exports "<DllPath>"

6, summed up some tips (subsequent supplements)

  C ++ calling Python acquisition PyObject*target, it can be more flexible parameter passing, here to do a summary of the learning process using the method.

  1, list data using the
  Python embedded in C / C ++ (Python core programming)
  Python list of data can be PyList_Size()re-acquired by size, PyList_GetItem()to obtain data of each bit.

PyObject* rel = PyObject_CallObject(func, args);  
int s = PyList_Size(rel);
for (int i = 0; i < s; i++)
{
	std::cout << PyUnicode_AsUTF8(PyList_GetItem(rel, i));
	std::cout  << std::endl;
}

  

  

  

Published 24 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/yangjf91/article/details/93062961