0

I am trying to run a python module using C++. I am using the instructions provided in https://docs.python.org/2/extending/embedding.html.

My C++ code:

#include <iostream>
#include <cmath>
#include <string>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <Python.h>
using namespace std;

int main(int argc, char **argv)
{
PyObject *pName, *pModule, *pDict, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    pName = PyString_FromString(argv[1]);
    /* Error checking of pName left out */

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, argv[2]);
        /* pFunc is a new reference */

        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(argc - 3);
            for (i = 0; i < argc - 3; ++i) {
                pValue = PyInt_FromLong(atoi(argv[i + 3]));
                if (!pValue) {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    fprintf(stderr, "Cannot convert argument\n");
                    return 1;
                }
                /* pValue reference stolen here: */
                PyTuple_SetItem(pArgs, i, pValue);
            }
            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            if (pValue != NULL) {
                printf("Result of call: %ld\n", PyInt_AsLong(pValue));
                Py_DECREF(pValue);
            }
            else {
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
                fprintf(stderr,"Call failed\n");
                return 1;
            }
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }
    Py_Finalize();
    return 0;
}

My python code:

def multiply(a,b):
    print "Will compute", a, "times", b
    c = 0
    for i in range(0, a):
        c = c + b
    return c

I compile using

g++ -static -I/usr/include/python2.7 -L/usr/lib/python2.7/config/ -o embed embed.cc -lpython2.7 -ldl -lm -lutil -lz -pthread

and run using

PYTHONPATH=. ./embed multiply multiply 1 2

This works fine for me. However, if i try importing the numpy module or any external module, it does not run crashing with

import numpy as np
  File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 153, in <module>
    from . import add_newdocs
  File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 6, in <module>
    from . import multiarray
ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyExc_SystemError
Failed to load "multiply"

Please advise me how to make it run! Edit: I tried including the dlopen("libpython2.7.so.1", RTLD_LAZY | RTLD_GLOBAL); line as advised in ImportError and PyExc_SystemError while embedding Python Script within C for PAM modules (.so files)

However, pModule is still not getting initialized!

Community
  • 1
  • 1
pd176
  • 821
  • 3
  • 10
  • 20
  • 1
    Possible duplicate of [ImportError and PyExc\_SystemError while embedding Python Script within C for PAM modules (.so files)](http://stackoverflow.com/questions/29880931/importerror-and-pyexc-systemerror-while-embedding-python-script-within-c-for-pam) – Michał Fita Nov 27 '15 at 10:56
  • I tried using the c++ code which OP in the link you posted said was working without errors. However it still doesn't recognize the test_func() module – pd176 Nov 27 '15 at 11:46
  • Have you tried looking at Py_SetPath as an option? – Willeman Jun 27 '16 at 16:16

0 Answers0