简体   繁体   中英

Boost python on mac os x lion with custom python

I'm trying to get boost python to work with a custom python library. I have a python source and build boost.python using :

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

then ./b2

but when I try to use boost.python in my application, I get

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

When I call PyRun_SimpleString("import sys\\nprint sys.version"); , I get 2.7.2, as I expect (and the version of python I build boost.python with; not the system version.)

Is there something I'm missing?

When I check what libraries the dylib is linked with, I get this:

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

In my Xcode target, I include the python from the --with-python-root argument folder and the content of boost/stage/lib,

链接库

I downloaded boost python and compiled it against my custom python installed using Mac Ports and it seems to be working just fine.

My steps...

$ /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'
>>> 

Additionally if you don't need be able to build everything yourself you can utilize mac ports to help you out. I haven't tried it, but it looked like boost.python was available, though version 1.47 instead of 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

Actually, in order to solve this we can take a look at our environment and compare if you still have problems :).

$ echo $press-TAB

Looks like you're linking boost python against the wrong version of python (2.7.0), and your application against the right version of python (2.7.2) - PyRun_SimpleString has nothing to do with boost python, but is a direct call into the Python API from your test app.

I use a project-config.jam file (in the boost build directory) to configure which python boost-python should be built with, including this line (for linking against my simple 2.7 installation):

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

First check

It may be a mismatch between the dylib that are linked and the include really used. One simple check is to pass the option --debug-configuration to bjam and to check the include and link paths:

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

should yield these lines in the beginning:

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"

you should see that interpreter, include and binaries match ( interpreter command , include path and library path respectively).

If there is a mismatch, check the python-config file in usr/bin : it should point to the python.org installation path. If not, the installer missed it and it should be fixed.

Second check

It may be that the library that is linked against is not properly set in /usr/lib . By a simple ls -al /usr/lib/*python* , you should see a /usr/lib/libpython2.7.dylib that points to your python.org installation. If not, the python.org installer missed it and it should be fixed.

Sorry for being a bit vague. I had this issue 1 year ago and fixed it by hand by doing many thing until it worked...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM