Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

Link Libraries

like image 767
sharvey Avatar asked Oct 09 '22 09:10

sharvey


2 Answers

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:          [email protected]

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

$ echo $press-TAB

like image 105
Derek Litz Avatar answered Oct 11 '22 22:10

Derek Litz


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/ ;
like image 36
James Avatar answered Oct 11 '22 21:10

James