ad_ad
ad_ad

Reputation: 385

Importing dependent python libraries from boost python

So I have created two modules using boost::python:

BOOST_PYTHON_MODULE(A) { ... }
BOOST_PYTHON_MODULE(B) { ... }

Such that B depends on A. I then try to call them using the python code:

import sys
sys.path.append('path/to/modules/')

import A
import B

... # python body

Finally I call the python script from terminal:

python path/to/python/script.py

This works perfectly as long as I execute the terminal command from the directory where I installed the boost::python modules. However, if I call it from any other directory I get the error

File "path/to/python/script.py", line 6, in <module>
    import B
 importError: dlopen(path/to/B.so, 2): Library not loaded: A.so
 Referenced from: path/to/B.so
 Reason: image not found

Notice, it fails on "import B" so the sys.path.append command is directing it to the correct place. For some reason the boost::python libraries don't look in the sys.path directories? Is there a way to set this? I tried adding the path to in boost python but this seems to only effect things at compile time of the libraries, not with python is running.

Does anyone know what to do?

Thanks!

Upvotes: 2

Views: 1031

Answers (1)

Tanner Sansbury
Tanner Sansbury

Reputation: 51871

If B.so links against A.so, then A.so needs to be in a path used by the dynamic linker when B.so is loaded. For example, before importing module B, add the path containing A.so to the LD_LIBRARY_PATH environment variable.

There is a distinction between a library and a module. A.so is a library, not a module. When attempting to import a module, Python may eventually try to load a library, requiring the library to initialize a module. In this case, library A.so initializes module A when it is loaded by the Python interpreter. The documentation for sys.path states that it specifies the search path for modules. Hence, import B will find B.so as part of the import and sys.path behavior. However, when the dynamic linker loads B.so, the dynamic linker, not the Python interpreter, requires resolution of A.so.

Consider consulting the dynamic linker's manual for more information about the paths checked when loading a library.

Upvotes: 2

Related Questions