[python] Adding a path to sys.path
Hi, 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.
On Wed, Oct 8, 2008 at 11:57 AM, Robert Dailey
Hi,
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; }
I tried the following code below as an alternative and it still does not work: using namespace boost::python; object sys( import( "sys" ) ); dict sysdict( sys.attr( "__dict__" ) ); list syspath( sysdict["path"] ); syspath.append( "C:\mypath" ); I could really use some help!
On Wed, Oct 8, 2008 at 10:10 PM, Robert Dailey
I tried the following code below as an alternative and it still does not work:
using namespace boost::python;
object sys( import( "sys" ) ); dict sysdict( sys.attr( "__dict__" ) ); list syspath( sysdict["path"] ); syspath.append( "C:\mypath" );
I could really use some help!
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
This is one way of doing it, works for me at least: python::str dir = "C:/somedir"; python::object sys = python::import("sys"); sys.attr("path").attr("insert")(0, dir); Thomas
On Wed, Oct 8, 2008 at 4:19 PM, Thomas Berg
This is one way of doing it, works for me at least:
python::str dir = "C:/somedir"; python::object sys = python::import("sys"); sys.attr("path").attr("insert")(0, dir);
This works great for me. Why didn't it work the way I was trying to do it?
participants (2)
-
Robert Dailey
-
Thomas Berg