【问题标题】:Boost.Python python linkage errorBoost.Python python链接错误
【发布时间】:2014-03-22 08:59:02
【问题描述】:

我正在运行具有最新 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

【问题讨论】:

    标签: python macos boost anaconda


    【解决方案1】:

    在尝试了我在网上找到的许多其他解决方案后,我失去了耐心,决定采用我自己的(非常糟糕的)hack。我创建了 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.shb2bjam,而无需提供/修改任何 Python 参数/选项

    【讨论】:

      【解决方案2】:

      我通过使用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"

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-17
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-17
        • 1970-01-01
        • 2017-07-04
        相关资源
        最近更新 更多