Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Calling Extended Embedding Python as shared library
#1
I am running Python3.6


I am trying to run the example:
https://docs.python.org/3.6/extending/embedding.html#extending-embedded-python

I would like do to the same thing using shared library. I changed the main(..) to runSomething(..).

I create a shared library from the file.

I created another file with main(..) calling runSomething(..).

SHARED LIBRARY
#include </usr/include/python3.6m/Python.h>


static int numargs=0;

/* Return the number of arguments of the application command line */
static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
    if(!PyArg_ParseTuple(args, ":numargs"))
        return NULL;
    return PyLong_FromLong(numargs);
}

static PyMethodDef EmbMethods[] = {
    {"numargs", emb_numargs, METH_VARARGS,
     "Return the number of arguments received by the process."},
    {NULL, NULL, 0, NULL}
};

static PyModuleDef EmbModule = {
    PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
    NULL, NULL, NULL, NULL
};

static PyObject*
PyInit_emb(void)
{
    return PyModule_Create(&EmbModule);
}

int runSomething(int argc, char *argv[]){
  
  numargs = argc;
  PyImport_AppendInittab("emb", &PyInit_emb);
  Py_Initialize();
  PyRun_SimpleString("import emb");
  PyRun_SimpleString("print('Number of arguments', emb.numargs())");
  Py_Finalize();

  return 0;

}
NEW MAIN FILE
int main(int argc, char *argv[]){
    runSomething();
    return 0;
}
How do I make this work?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Embedding return in a print statement Tapster 3 173 Oct-07-2019, 03:10 PM
Last Post: Tapster
  More Python Embedding Trouble jibarra 3 379 Jul-11-2019, 09:25 PM
Last Post: Gribouillis
  Python Embedding jibarra 4 404 Jul-11-2019, 03:25 PM
Last Post: jibarra
  Embedding or adding IDE like "repl" inside Flask app sray 1 270 Jul-03-2019, 03:13 PM
Last Post: nilamo
  Embedding Python into a simulator siggi 0 440 Apr-24-2019, 07:42 PM
Last Post: siggi
  Python calling function unexpectedly lucaswinnerton 1 401 Jan-24-2019, 10:40 PM
Last Post: stullis
  Embedding, windows, and virtual environments mariofutire 0 737 Sep-14-2018, 10:30 AM
Last Post: mariofutire
  calling ".sql" files in python raopatwari 3 5,790 Jan-18-2018, 07:33 PM
Last Post: nilamo
  calling python function with multiple argument in C# braveYug 1 3,573 Nov-14-2017, 11:39 AM
Last Post: Larz60+
  [CPP] Embedding Python and use GDB Razazel 4 1,761 Nov-12-2017, 05:59 PM
Last Post: Razazel

Forum Jump:


Users browsing this thread: 1 Guest(s)