Bottom Page

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

I am trying to run the example:

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(..).

#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,

static PyObject*
    return PyModule_Create(&EmbModule);

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

  return 0;

int main(int argc, char *argv[]){
    return 0;
How do I make this work?

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  More Python Embedding Trouble jibarra 3 217 Jul-11-2019, 09:25 PM
Last Post: Gribouillis
  Python Embedding jibarra 4 261 Jul-11-2019, 03:25 PM
Last Post: jibarra
  Embedding Python into a simulator siggi 0 268 Apr-24-2019, 07:42 PM
Last Post: siggi
  Python calling function unexpectedly lucaswinnerton 1 296 Jan-24-2019, 10:40 PM
Last Post: stullis
  Embedding, windows, and virtual environments mariofutire 0 532 Sep-14-2018, 10:30 AM
Last Post: mariofutire
  Shared queues l00p1n6 3 659 May-15-2018, 01:38 PM
Last Post: DeaD_EyE
  calling ".sql" files in python raopatwari 3 4,469 Jan-18-2018, 07:33 PM
Last Post: nilamo
  calling python function with multiple argument in C# braveYug 1 2,858 Nov-14-2017, 11:39 AM
Last Post: Larz60+
  [CPP] Embedding Python and use GDB Razazel 4 1,505 Nov-12-2017, 05:59 PM
Last Post: Razazel
  calling python function in c++ callback getting segmentation fault error Jotirling 3 2,483 Oct-26-2017, 08:55 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)