Cython cannot find shared objects file

I am trying to link my own C library with Cython following the directions I found online, including this answer:

Using Cython to link Python to a shared library

I am running IPython through Spyder.

My setup.py looks like this:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy as np

setup(
  ext_modules = cythonize(
      [Extension("*",["*.pyx"],
                 libraries =["MyLib"],
                 extra_compile_args = ["-fopenmp","-O3"],
                 extra_link_args=["-L/path/to/lib"])
                 ]),
  include_dirs = [np.get_include()],
)

      

The file libMyLib.so

is in /path/to/lib

and it compiles just fine.

I have a Python script in the IPython profile startup folder that does this

try:
  os.environ["LD_LIBRARY_PATH"] += ":/path/to/lib"
except KeyError:
  os.environ["LD_LIBRARY_PATH"] = "/path/to/lib"

      

I can confirm that this works because if I type os.environ["LD_LIBRARY_PATH"]

into the IPython interpreter it returns/path/to/lib

But when I try to load the Cython module (i.e. import mycythonmodule

) I get:

ImportError: libMyLib.so: cannot open shared object file: No such file or directory

      

I also tried putting libMyLib.so elsewhere to see if cython finds:

  • In the directory where Python is running
  • On the Python Path
  • In the same folder as the cython module

But it still doesn't find the shared library. The only way to get it to find the library is to dump it in /usr/lib

, but I don't want it there, I want to be able to set the library path.

Did I miss something?

+3


source to share


1 answer


I answer myself in case anyone is facing the same problem. Looks like the answers are here:

Set LD_LIBRARY_PATH before import in python

Change LD_LIBRARY_PATH at runtime for ctypes



According to these answers (and my experience) the linker reads LD_LIBRARY_PATH when python starts up, so changing it from within python has no beneficial effect, at least not the effect I was hoping for. The only solution is to either wrap python in a wrapper script that sets LD_LIBRARY_PATH, or cut the shared object somewhere in the linker search path.

Kind of pain, but that's what it is.

+3


source







All Articles