2

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!

2
  • Why do you want to dlopen the lib? You can setup the dependencies in compile time Commented Sep 11, 2013 at 15:12
  • @balki: The dlopen is part of the import behavior. Without statically linking extensions in the interpreter, how else would the interpreter for a dynamic language load a C++ library? Commented Sep 11, 2013 at 16:39

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.