On Wed, Oct 8, 2008 at 11:57 AM, Robert Dailey <rcdailey@gmail.com> wrote:

I was wondering if the code below is correct for adding a path to sys.path in Python. Note that this will occur before any modules have been executed/imported through Boost.Python. I'm also embedding the interpreter, which is why I am doing this. I'm not sure how it can obtain the sys.__dict__ when I haven't imported sys, so explanation on this would be helpful for me.

            using namespace boost::python;
            using namespace boost::filesystem;

            object sys = GetNamespace( "sys" );

            list syspath( sys["path"] );
            syspath.insert( 0, complete( path ).file_string() );
            sys["path"] = syspath;

Note that "path" is a boost::filesystem::path object containing the path I wish to add to sys.path.

Forgot to provide the definition of GetNamespace(). It is below:

    boost::python::dict GetNamespace( char const* mod )
        using namespace boost::python;
        dict moduleNamespace( import( mod ).attr( "__dict__" ) );
        return moduleNamespace;