I’m trying to run a Python function from Erlang using a C++ driver. I am able to run the Python function outside of Erlang by calling the C++ driver function and everything works perfectly, but when I try to call it from Erlang, I get the following import error:
This error happens when the C++ driver runs the PyImport_Import() function on the Python file. It looks like the shared library mentioned in the error is trying to access the “Python.h” file and can’t find it. I tried adding its location to the PATH environment variable (using Py_SetPath()) but that did not solve the issue.
My C++ driver uses the “Python.h” file, but I have to include the path at compile time or else I get a similar import error.
Is there a specific environment variable I need to set in my rebar3.config file or elsewhere to allow the mentioned shared library to find the “Python.h” file? Or is there something else I’m doing (or not doing) that is causing this error?
It sounds like your LD_LIBRARY_PATH simply doesn’t have /######/lib/python3.8/site-packages/grpc/_cython/ on it. Make sure this is set before launching from erlang.
@starbelly, thanks for responding. I did try setting that variable in my C++ driver (using setenv) before running Py_Initialize(), but that didn’t fix the issue. Do I need to set it in a different place?
Ok, I tried running export LD_LIBRARY_PATH=/######/lib/python3.8/site-packages/grpc/_cython/ before running rebar3 shell, but I’m still getting the error. I also tried doing the same thing but setting LD_LIBRARY_PATH to the location of Python.h but that didn’t work either.
Not really Maybe say so next time to be clear. It sounds like you’re still missing some libs on your LD_LIBRARY_PATH. Now that I look back at the error, you’re missing part of python itself on your LD_LIBRARY_PATH. This could also be due to linking problems. I’d do some googling on the error
I have spent about a week googling the error and trying various things with no success. What makes me think it is Erlang related is the fact that the code runs fine outside of Erlang (calling the driver functions directly from C++). It’s when I try to run it from inside Erlang that I get the error.
What’s weird is the fact that my C++ driver uses libpython to setup and start the Python interpreter, so it has to be loaded. But it seems as though inside the Python interpreter, it can’t access it.
In normal operation (from Erlang), I load the driver, start the driver, and then do a port_control call to execute Python call, although I haven’t gotten past starting the driver, since that’s where I run into the issue.
The C++ driver just returns 0 for the init call. In the start call, it does a Py_SetPath, Py_Initialize, and then attempts the PyImport_Import at which point it runs into the import error. I have tried adding the path to libpython in the Py_SetPath command as well, with no luck.
And just to reiterate, when I run the C++ driver outside of Erlang, all is well. But when I try to run it from Erlang, I get this issue.