Am I running this correctly? - 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: Am I running this correctly? (/thread-19892.html) |
Am I running this correctly? - jibarra - Jul-18-2019 I have a few qeustions. I am running Python3.6 Working on this exmaple: https://docs.python.org/3.6/extending/embedding.html#extending-embedded-python What exactly do they mean by extending the Python Interpreter? Can I call the import statement anyother way besides using PyRun_SimpleString, as shown below in the int main() function? import emb print('Number of arguments', emb.numargs()) #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 main(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; }Please advise |