python - Boost.Python python 链接错误

标签 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 个 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 参数/选项

关于python - Boost.Python python 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22575527/

相关文章:

python - Alembic 和枚举类型

python - 通过将两个长度不等的列表压缩在一起来创建字典

python - 使用自定义小部件 kivy

c++ - xcode c/c++ 链接器错误 : undefined symbol

c++ - 如何使用自定义 boost::log 格式化程序输出 TimeStamp 和 ThreadID 属性?

c++ - 将元素插入排序数组并找到其索引的最有效方法

python - Airflow stack webserver 无法解析 postgres 相关属性,无法启动

c++ - 如何在 mac osx 中隐藏终端窗口?

macos - 如何将文本添加到文件夹中所有文件的开头?

c++ - boost split 用一个字符或一个字符串