7

I recently installed Boost using MacPorts, with the intent to do some Python embedding in C++. I then decided to check if I configured Xcode correctly with an example found on Python's website:

#include <boost/python.hpp>

using namespace boost::python;

int main( int argc, char ** argv ) 
{
    try 
    {
        Py_Initialize();

        object main_module(handle<>(borrowed(PyImport_AddModule("__main__"))));

        object main_namespace = main_module.attr("__dict__");

        handle<> ignored(PyRun_String("print \"Hello, World\"",
                                      Py_file_input,
                                      main_namespace.ptr(),
                                      main_namespace.ptr()));
    } 
    catch( error_already_set ) 
    {
        PyErr_Print();
    }
}

It compiles correctly, but when I launch it, the call to attr() throws an exception, and the resulting error message is "TypeError: attribute name must be string, not 'str'". Which suspiciously sounds like a placeholder.

I looked it up on Google, but no luck.

I use Boost v1.39, Python 2.5 and GCC 4.0, on Leopard.

2
  • Hard to say what's wrong, code seems to work (win/lin, py3, gcc/vs2005/10, boost 42/43) did you check whether your version of OS is officially supported by everything that you use? Commented Jun 23, 2010 at 20:31
  • Python, Boost and GCC 4 are all officially supported on OS X. However, it's hard to tell if Boost.Python specifically is. Their documentation seems outdated, and it seems that it worked on OS X 10.3 with Python 2.3 and GCC 3.3, but nothing about Leopard. Perhaps that's the problem. I'll look into it. Commented Jun 23, 2010 at 21:23

1 Answer 1

1

Your code worked for me with the following configuration:

  • Snow Leopard
  • gcc version 4.2.1 (AppleInc. build 5646)
  • Boost 1.41.0 installed to /usr/local/boost/1_41_0/
  • Stock OSX Python 2.5

Compiled using:

g++ -I/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/ -I/usr/local/boost/1_41_0/include -L/usr/local/boost/1_41_0/lib/ -boost_python -L/usr/lib/python2.6/config -lpython2.6 test.cpp

Sign up to request clarification or add additional context in comments.

1 Comment

I tried with Boost 1.42, GCC 4.2 and Python 2.5; still does not work. I have yet to test it with Snow Leopard...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.