Boost.Python python链接错误

时间:2014-03-22 08:59:02

标签: python macos boost anaconda

我使用最新的Boost发行版(1.55.0)运行Mac OS X 10.8.4(Darwin 12.4.0)。我按照here的说明构建了我的发行版中包含的Boost-Python项目教程,并且构建得很好。

尽管如此,输出编译库依赖于Mac的系统Python,而不是我试图链接到的anaconda Python:

[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)

我已经尝试了以下配置,但它们似乎都没有改变使用哪个Python:

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

# 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

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

无论如何,我总会得到这样的信息:

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

这是系统Python调用的比较:

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

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

2 个答案:

答案 0 :(得分:0)

在尝试了我在网上找到的许多其他解决方案后,我失去了耐心并决定采用自己的(非常糟糕的)黑客攻击。我创建了2个bash脚本,一个用于将sys的Python链接到Anaconda,另一个用于重新链接到原始的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


运行ana_py.sh后,您可以投放bootstrap.shb2和& bjam没有提供/修改任何Python参数/选项

答案 1 :(得分:0)

我使用install-name-tool更改从属dylib的名称来解决此问题:

  1. 更改libboost_python.dylib的权限:

    chmod +w libboost_python.dylib

  2. 然后更改从属dylib

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

  3. 希望它有用。