Python Forum
Embedded python in C - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Embedded python in C (/thread-5898.html)



Embedded python in C - alxhoff - Oct-27-2017

Hi all,

I am currently writing a program in C where I need to embed a python script as it is part of a testbench that will be comparing many implementation of Datalog in various language. Beside the point.

I have been following this documentation (section 5.3) for an example of how to embed a script into C via calling a function. The example uses input arguments to get the information but I will be needing to hardcode the filenames etc so I have the various properties hardcoded into char* etc.

My current implementation boils down to

Quote://globals
char* python_script_file = "python_queens.py";

//in my run benchmark function
Py_Initialize(); //init python

PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;

pName = PyString_FromString(python_script_file); //prepare file path

pModule = PyImport_Import(pName); <<< CULPRIT

--** the rest of the implementation is commented out as it crashes on the above line **--

Py_Finalize();


I am getting the following memory error:

Quote: “./TARS” terminated by signal SIGSEGV (Address boundary error)

The script is called python_queens.py and resides in the same directory as the executable. I have tried both

Quote: char* python_script_file = "python_queens.py";

and

Quote: char* python_script_file = "python_queens";

As I was not sure if the file extension needed to be included. I am quite sure it has to do with the filename I am passing in but I am not sure what I am missing.

Any help would be appreciated.

Cheers

I have since tried this example and also had it seg fault on me. I copied his exact two file and made the following CMake file for it but it still does not work.

Quote:project(pythontest)
cmake_minimum_required (VERSION 3.4 FATAL_ERROR)
set(CMAKE_BUILD_TYPE Debug)

#packages
find_package(PythonLibs 2.7 REQUIRED)

set(PYTHON_EXECUTABLE "/usr/bin/python2.7")
set(PYTHON_INCLUDE_DIR "/usr/include/python2.7")
set(PYTHON_LIBRARIES "/usr/lib/x86_64-linux-gnu/libpython2.7.so")

#includes
include_directories(${PYTHON_INCLUDE_DIRS})

#source files
file(GLOB SOURCES "*.c")

add_executable(call_function ${SOURCES})
target_link_libraries(call_function ${PYTHON_LIBRARIES})

Executing via
Quote:./call_function py_function multiply



RE: Embedded python in C - alxhoff - Oct-27-2017

Found the solution here.
Quote:There were several problems with your code:

Py_Initialize() was not called.
PyImport_ImportModule() failed to find your python file, since in embedded Python you start without an initial module, relative to which the search can work. The fix is to explicitly include the current directory in sys.path.
"(O)" in Py_BuildValue() should use a capital 'O'.
The printlist function should return a value (since that is what the C-code expects).
This should work:


main.c
#include <Python.h>

void initPython()
{
Py_Initialize();
PyObject *sysmodule = PyImport_ImportModule("sys");
PyObject *syspath = PyObject_GetAttrString(sysmodule, "path");
PyList_Append(syspath, PyString_FromString("."));
Py_DECREF(syspath);
Py_DECREF(sysmodule);
}

int callModuleFunc(int array, size_t size) {
PyObject *mymodule = PyImport_ImportModule("py_function");
assert(mymodule != NULL);
PyObject *myfunc = PyObject_GetAttrString(mymodule, "printlist");
assert(myfunc != NULL);
PyObject *mylist = PyList_New(size);
for (size_t i = 0; i != size; ++i) {
PyList_SET_ITEM(mylist, i, PyInt_FromLong(array[i]));
}
PyObject *arglist = Py_BuildValue("(O)", mylist);
assert(arglist != NULL);
PyObject *result = PyObject_CallObject(myfunc, arglist);
assert(result != NULL);
int retval = (int)PyInt_AsLong(result);
Py_DECREF(result);
Py_DECREF(arglist);
Py_DECREF(mylist);
Py_DECREF(myfunc);
Py_DECREF(mymodule);
return retval;
}

int main(int argc, char *argv)
{
initPython();
py_function.py
'''py_function.py - Python source designed to '''
'''demonstrate the use of python embedding'''

def printlist(mylist):
print mylist
return 0