简体   繁体   中英

Impose linking to specific python library on OSX Lion

I am building a python module from c++ files, using swig, and I also have a local build of python 2.7 in my home directory, on OSX 10.8. I want to build with my local python, but osx has a system python, and that causes some trouble. I get the following error

>>> import example
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6

which according to browsing around, it's due to my python executing, but the module linking against the system python.

I deployed a small swig example, as from tutorial , and then linked as follows

gcc -shared -I builddir/include/python2.7/ example.c example_wrap.c \
     -o _example.so -L builddir/lib/python2.7/ -lpython2.7

Sure enough, the _example.so keeps binding against the system python, probably because frameworks come first in the resolution, although I am not sure if this is at linking time or at execution time.

_example.so:
    _example.so (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.2)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

how can I force it to bind against my own python library?

Edit : I successfully managed to make it import by doing a

 install_name_tool -change 
       /System/Library/Frameworks/Python.framework/Versions/2.7/Python
       @executable_path/../lib/libpython2.7.dylib

but it feels like a hack. Can't I just let the linker find the library for me and put the appropriate reference in it? Can't I just specify the library name (libpython2.7.dylib) and let it resolve at runtime as it happens in Linux ?

You will need to test the solution, because I don't have tan OSX machine to test it here, but I think this can solve your problem:

Create a (or edit one) run script and change your alias or your PATH variable:

alias python='/your/local/python'
PATH=/your/local/python:$PATH

# Run your app here

Let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM