Boost.Python python linkage error

2019-07-05 12:41发布

问题:

I'm running Mac OS X 10.8.4 (Darwin 12.4.0) with the lastest Boost distribution (1.55.0). I'm following the instructions here to build the tutorial Boost-Python project included in my distribution, and it builds fine.

Nonetheless, the output compiled libraries depend on Mac's system Python and not the anaconda Python that I'm trying to link to:

[00:20] [tutorial] $ otool -L libboost_python.dylib
libboost_python.dylib:
    libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0)
    libpython2.7.dylib (compatibility version 2.7.0, current version 2.7.0)
    /opt/local/lib/libgcc/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.18.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
    /opt/local/lib/libgcc/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)

[00:20] [tutorial] $ otool -L /usr/lib/libpython2.7.dylib
/usr/lib/libpython2.7.dylib:
    /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)

I've tried the following configurations, but none of them seem to change which Python to use:

$BOOST_ROOT/bootstrap.sh --with-python=$ANACONDA_PATH/bin/python

or

# Here, I've explicitly chosen Anaconda-provided libpython2.7.dylib
# $BOOST_ROOT/stage/lib/libboost_python.dylib refers to the dynamic
# version of boost_python.
sudo g++ -I$BOOST_ROOT -I$ANACONDA_PATH/include -L$ANACONDA_PATH/lib
    -lpython2.7 $BOOST_ROOT/stage/lib/libboost_python.dylib
    hello.cpp -o hello_ext.so

or

$BOOST_ROOT/bjam python=$ANACONDA_PATH/bin/python

No matter what, I'll always get this message:

[01:58] [tutorial] $ python hello.py
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6

Here's the system Python call in comparison:

[01:58] [tutorial] $ /usr/bin/python hello.py
hello, world

Similar: Homebrew + Python on mac os x 10.8: Fatal Python error: PyThreadState_Get: no current thread importing mapnik

回答1:

After trying many other solutions I found on the web, I lost patience and decided to go with my own (very-bad) hack. I've created 2 bash scripts, one to link the sys's Python to Anaconda's, and one to relink back to the original Python:

ana_py.sh:

#!/usr/bin/env bash
# Link to Anaconda's Python
# $ANACONDA_PATH is the path to your anaconda folder

# BIN
cd /usr/bin

if [ ! -h python ]; then
    sudo mv python python_orig;
else
    sudo unlink python;
fi
sudo ln -s $ANACONDA_PATH/bin/python python

if [ ! -h python-config ]; then
    sudo mv python-config python-config_orig;
else
    sudo unlink python-config;
fi
sudo ln -s $ANACONDA_PATH/bin/python-config python-config

# INCLUDE
cd /usr/include

sudo unlink python2.7
sudo ln -s $ANACONDA_PATH/include/python2.7 python2.7

# LIB
cd /usr/lib

sudo unlink python2.7
sudo unlink libpython2.7.dylib
sudo ln -s $ANACONDA_PATH/lib/python2.7 python2.7
sudo ln -s $ANACONDA_PATH/lib/libpython2.7.dylib libpython2.7.dylib


sys_py.sh:

#!/usr/bin/env bash
# Link to Mac OSX Python

# BIN
cd /usr/bin

sudo unlink python
if [ -f python_orig ]; then
    sudo mv python_orig python;
else
    sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python python;
fi

sudo unlink python-config
if [ -f python-config_orig ]; then
    sudo mv python-config_orig python-config;
else
    sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python-config python-config;
fi

# INCLUDE
cd /usr/include

sudo unlink python2.7
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 python2.7

# LIB
cd /usr/lib

sudo unlink python2.7
sudo unlink libpython2.7.dylib
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 python2.7
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/Python libpython2.7.dylib


Once you've run ana_py.sh, you can run bootstrap.sh, b2, & bjam without providing/modifying any of their Python parameters/options



回答2:

I solve this problem by using install-name-tool to change the name of the dependent dylib:

  1. To change the permission of your libboost_python.dylib:

    chmod +w libboost_python.dylib

  2. Then change the dependent dylib:

    install_name_tool -change libpython2.7.dylib /path/to/anaconda/lib/libpython2.7.dylib "libboost_python.dylib"

Hope it is helpful.