1

I am venturing into the land of creating C/C++ bindings for Python using pybindgen. I've followed the steps outlined under "Building it ( GCC instructions )" to create bindings for the sample files:

http://packages.python.org/PyBindGen/tutorial.html#a-simple-example

Running make produces a .so file. If I understand how .so files work, I should be able to import the classes in the shared object into Python. However, I'm not sure where to place the file and how to let Python know where it is. Additionally, do the original c/c++ source files need to accompany the .so file?

So far I've tried placing the file in /usr/local/lib and adding that path to DYLD_LIBRARY_PATH to the .bash_profile. When I try to import the module from within the Python interpeter an error is thrown stating that the module can not be found.

So, my question is: What needs to be done with the generated .so file in order for it to be used by a Python program?

JeremyFromEarth
  • 14,344
  • 4
  • 33
  • 47

2 Answers2

3

Python looks for .so modules in the same directories where it searches python ones. So you have to install it as you would normal python module either somewhere that is on python's sys.path by default (/usr/share/python/site-lib or something like that—it'd distribution-dependent) or add the directory to PYTHONPATH environment variable.

It's python that's loading the module using dlopen, not the dynamic linker, so LD_LIBRARY_PATH (note, there is no DY) won't help you.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • Thanks for your input. I've placed the .so file in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. This is definitely on the path as I'm using other libraries in the same directory. However, Python is still throwing an error stating that no such module exists. Perhaps there is something wrong with the .so file? As for the discrepancy regarding LD_LIBRARY_PATH, I'm on a Mac and I read here http://www.agavemountain.com/2008/03/ldlibrarypath-in-mac-os-x.html that on a mac the env variable is called DYLD. – JeremyFromEarth May 11 '11 at 07:34
0

Same as all other Python modules. It must be within one of the locations given in sys.path.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358