【问题标题】:Boost python on mac os x lion with custom python使用自定义 python 在 mac os x lion 上提升 python
【发布时间】:2012-01-07 04:33:03
【问题描述】:

我正在尝试让 boost python 与自定义 python 库一起工作。我有一个 python 源代码并使用以下方法构建 boost.python:

./bootstrap.sh --with-python-root=../Python-2.7.2 --with-libraries=python

然后./b2

但是当我尝试在我的应用程序中使用 boost.python 时,我得到了

Fatal Python error: Interpreter not initialized (version mismatch?)

当我调用 PyRun_SimpleString("import sys\nprint sys.version"); 时,我得到了 2.7.2,正如我所料(以及我构建 boost.python 所用的 python 版本;不是系统版本。)

我有什么遗漏吗?

当我检查 dylib 与哪些库链接时,我得到了这个:

libboost_python.dylib (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.1)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)`

在我的 Xcode 目标中,我包含来自 --with-python-root 参数文件夹的 python 和 boost/stage/lib 的内容,

【问题讨论】:

    标签: c++ python xcode macos boost-python


    【解决方案1】:

    我下载了 boost python 并针对我使用 Mac Ports 安装的自定义 python 编译它,它似乎工作得很好。

    我的步骤...

    $ /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python
    Python 2.7.2 (default, Nov 17 2011, 00:52:26)    
    $ sudo ./bootstrap.sh --with-libraries=python --with-python-root=/opt/local/Library/Frameworks/Python.framework/Versions/2.7
    $ ./b2
    $ cd /Users/YourName/Downloads/boost_1_48_0/libs/python/example/tutorial
    $ ../../../../bjam 
    ...patience...
    ...patience...
    ...found 1577 targets...
    ...updating 12 targets...
    common.mkdir bin
    common.mkdir bin/darwin-4.2.1
    common.mkdir bin/darwin-4.2.1/debug
    darwin.compile.c++ bin/darwin-4.2.1/debug/hello.o
    darwin.link.dll bin/darwin-4.2.1/debug/hello_ext.so
    common.copy libboost_python.dylib
    common.copy hello_ext.so
    common.mkdir bin/hello.test
    common.mkdir bin/hello.test/darwin-4.2.1
    common.mkdir bin/hello.test/darwin-4.2.1/debug
    capture-output bin/hello.test/darwin-4.2.1/debug/hello
    **passed** bin/hello.test/darwin-4.2.1/debug/hello.test
    ...updated 12 targets...
    $ ls
    Jamroot         hello.cpp       hello_ext.so
    bin         hello.py        libboost_python.dylib
    $ python
    Python 2.7.2 (default, Nov 17 2011, 00:52:26) 
    [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import hello_ext
    >>> hello_ext.greet()
    'hello, world'
    >>> 
    

    此外,如果您不需要自己构建所有东西,您可以利用 mac 端口来帮助您。我还没有尝试过,但它看起来像 boost.python 可用,虽然版本是 1.47 而不是 1.48。

    $ port info boost
    boost @1.47.0, Revision 2 (devel)
    Variants:             debug, no_single, no_static, openmpi, python24, python25, python26, python27, python31, python32, regex_match_extra,
                          universal
    
    Description:          Boost provides free portable peer-reviewed C++ libraries. The emphasis is on portable libraries which work well with
                          the C++ Standard Library.
    Homepage:             http://www.boost.org
    
    Library Dependencies: zlib, expat, bzip2, icu
    Platforms:            darwin
    License:              Boost-1.0
    Maintainers:          adfernandes@macports.org
    

    其实,为了解决这个问题,我们可以看看我们的环境,比较一下是否还有问题:)。

    $ echo $press-TAB

    【讨论】:

      【解决方案2】:

      看起来您将 boost python 与错误版本的 python (2.7.0) 相关联,而您的应用程序与正确版本的 python (2.7.2) 相关联 - PyRun_SimpleString 与 boost python 无关,但是是从您的测试应用直接调用 Python API。

      我使用project-config.jam 文件(在 boost 构建目录中)来配置应该使用哪个 python boost-python 构建,包括这一行(用于链接我的简单 2.7 安装):

      using python : 2.7 : /Library/Frameworks/Python.framework/Versions/2.7/ ;
      

      【讨论】:

      • 感谢您的回答。我的 project-config.jam 包含 using python : 2.7 : ../Python-2.7.2/ ;,但 otool 仍然列出了 /System/Library/Framework 条目。
      • @sharvey 我想你已经尝试过干净的构建,指定绝对路径,using python : 2.7.2 : ... 等等?
      • 我不能输入2.7.2,它给了我一个警告说:Warning: "using python" expects a two part (major, minor) version number; got 2.7.2 instead。使用绝对路径不会改变任何东西。在 Xcode 和 ./b2 --clean 中清理仍然不能解决问题...
      • 嗯。好吧,肯定是 boost python 链接到了错误的版本。您没有覆盖您的版本的 user-config.jam 文件吗?也许值得将在构建和安装它时 boost python 正在使用的 /System/Library/Frameworks/Python.framework/Versions/2.7 python 放在一边。
      【解决方案3】:

      第一次检查

      可能是链接的 dylib 和实际使用的 include 不匹配。一项简单的检查是将选项 --debug-configuration 传递给 bjam 并检查包含和链接路径:

      ./b2 -q --prefix=your_install_dir YOU_OTHER_OPTIONS --debug-configuration install
      

      应该在开头产生这些行:

      notice: [python-cfg] Configuring python...
      notice: [python-cfg]   user-specified version: "2.7"
      notice: [python-cfg]   user-specified cmd-or-prefix: "/Library/Frameworks/Python.framework/Versions/2.7"
      notice: [python-cfg] Checking interpreter command "/Library/Frameworks/Python.framework/Versions/2.7/bin/python"...
      notice: [python-cfg] running command '/Library/Frameworks/Python.framework/Versions/2.7/bin/python -c "from sys import *; print('version=%d.%d\nplatform=%s\nprefix=%s\nexec_prefix=%s\nexecutable=%s' % (version_info[0],version_info[1],platform,prefix,exec_prefix,executable))" 2>&1'
      notice: [python-cfg] ...requested configuration matched!
      notice: [python-cfg] Details of this Python configuration:
      notice: [python-cfg]   interpreter command: "/Library/Frameworks/Python.framework/Versions/2.7/bin/python"
      notice: [python-cfg]   include path: "/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7"
      notice: [python-cfg]   library path: "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config" "/Library/Frameworks/Python.framework/Versions/2.7/lib"
      notice: [python-cfg] framework directory is "/Library/Frameworks/Python.framework"
      

      您应该看到解释器、包含和二进制文件匹配(分别为interpreter commandinclude pathlibrary path)。

      如果不匹配,检查usr/bin中的python-config文件:它应该指向python.org的安装路径。如果没有,安装程序错过了它,应该修复它。

      二次检查

      可能是链接到的库在/usr/lib 中设置不正确。通过简单的ls -al /usr/lib/*python*,您应该会看到指向您的python.org 安装的/usr/lib/libpython2.7.dylib。如果没有,python.org 安装程序错过了它,应该修复它。

      抱歉有点含糊。一年前我遇到了这个问题,并通过做很多事情手动修复它,直到它起作用......

      【讨论】:

        猜你喜欢
        • 2012-04-05
        • 2013-03-04
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多