Python Forum
Calling Extended Embedding Python as shared library
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/em...ded-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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Embedding a python file online Dreary35 0 188 Jun-10-2021, 05:05 PM
Last Post: Dreary35
  Calling python from c++ in visual studio pdk5 0 341 May-24-2021, 10:18 AM
Last Post: pdk5
  Can Embedded Python run any shared library on Android ? sprotz 0 503 Nov-08-2020, 12:21 PM
Last Post: sprotz
  Calling Oracle REST SQL from Python johnjacob 2 521 Nov-05-2020, 04:19 AM
Last Post: johnjacob
  ImportError: Can not find the shared library: libhdfs3.so aupres 1 853 Oct-18-2020, 06:57 AM
Last Post: bowlofred
  Embedding python cause crash when use boost::asio multi threading udvatt108 0 532 Oct-04-2020, 03:15 PM
Last Post: udvatt108
  Need help merging/embedding duckredbeard 10 1,047 Aug-13-2020, 04:48 AM
Last Post: duckredbeard
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 1,604 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 664 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  Embedding return in a print statement Tapster 3 863 Oct-07-2019, 03:10 PM
Last Post: Tapster

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020