Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost.Python on Mac OS X: "TypeError: Attribute name must be string"

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.

like image 438
Etienne de Martel Avatar asked Jun 22 '10 01:06

Etienne de Martel


1 Answers

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

like image 104
synthesizerpatel Avatar answered Sep 23 '22 08:09

synthesizerpatel