I am using a C++ library which can be built as a Python module using SWIG.
Building the module results in a C++ shared object, call it libFoo.so
, and a Python module which includes both a Python file, Foo.py
, and some shared object which is used by Python to interface with libFoo.so
, which is called _Foo.so
.
Now I can sudo make install
both the C++ library and the Python module and everything works fine if load Python and do import Foo
.
If I sudo make install
the C++ library, and then install the Python module into the virtualenv (by tweaking the Makefile so that it uses the virtualenv version of Python to run setup.py
), everything also works. If I run Python from the venv, I can import Foo and the C++ library is correctly loaded. Outside the venv I can't import Foo.
Now, I can build and install the C++ library so that it installs itself into venv/lib
. I can then correctly build the Python package and install it into the virtualenv. If I run the virtualenv version of Python, importing Foo fails because of a missing symbol, which seems to mean that it found Foo.py
and _Foo.so
, but not libFoo.so
.
If I do
LD_PRELOAD=venv/lib/libFoo.so python
the import succeeds and the library correctly works. (Note that there doesn't seem to be anything special about the directory venv/lib
in this case. I could equally have installed the library somewhere completely different and passed in that filename.)
However, I can't get Python to look in the folder venv/lib for the C++ shared object when trying to load that library. Setting LD_LIBRARY_PATH
doesn't seem to change anything, I believe because either the virtualenv version of Python ignores this variable, or because there are settings in the Python module itself which determine how the C++ object should be loaded.
Is there some way of correctly installing a C++ or C library to the virtualenv so that Python code which is run in that virtual environment correctly loads it? Or is this just something which virtualenvs were not designed to do?
(I don't think the mechanics of this question are dependent on SWIG, but if SWIG has any implications, or if there are techniques specifically related to SWIG that could solve my problem, that would be great to know.)