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
  python script is hanging while calling a procedure in database prasanthi417 4 503 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 814 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  Embedding python script into html via pyscript pyscript_dude 7 1,548 Apr-16-2023, 11:17 PM
Last Post: pyscript_dude
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,037 Sep-19-2022, 02:32 AM
Last Post: Xeno
  C++ python embedding comarius 0 825 Aug-26-2022, 02:01 AM
Last Post: comarius
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,023 Jan-07-2022, 04:55 PM
Last Post: bowlofred
Question Embedding a python file online Dreary35 0 1,520 Jun-10-2021, 05:05 PM
Last Post: Dreary35
  Calling python from c++ in visual studio pdk5 0 2,156 May-24-2021, 10:18 AM
Last Post: pdk5
  Can Embedded Python run any shared library on Android ? sprotz 0 2,309 Nov-08-2020, 12:21 PM
Last Post: sprotz
  Calling Oracle REST SQL from Python johnjacob 2 2,031 Nov-05-2020, 04:19 AM
Last Post: johnjacob

Forum Jump:

User Panel Messages

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