boost::python embedding external script file

This is a discussion on boost::python embedding external script file within the C++ Programming forums, part of the General Programming Boards category; I'm trying to add some scripting capability to a program using boost: ython, and I'm having trouble getting it to ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,349

    boost::python embedding external script file

    I'm trying to add some scripting capability to a program using boost:ython, and I'm having trouble getting it to load the script file. not surprisingly, there are no examples in the boost docs for exactly how to do this, so I'm basically winging it.

    for the record, I hate the boost docs. they only give the most basic description of how something works within the context of boost-only code. if there is other initialization that needs to be done (Py_Initialize()), they do not mention it at all. I've found this to be the case with most of the boost tutorials and documentation that I have used. I had to accidentally find that little bit of info on a stack overflow article, which magically made my program stop crashing.

    in any case, here's some code for you to ponder:
    main.cpp:
    Code:
    int main(int argc, char *argv[])
    {
      Py_Initialize();
      PySys_SetArgv(argc, argv);
      ...
    }
    the_file_that_is_supposed_to_load_the_script.cpp
    Code:
    try
    {
    	boost::python::object main_module = boost::python::import("__main__");
    	boost::python::object main_namespace = main_module.attr("__dict__");
    	//boost::python::object output = boost::python::exec(StrFormat("import test\ntest.StandardInvoiceMethod({0}, \"{1}\", \"{2}\")", siteid, startDate, endDate).c_str(), main_module);
    
    	std::string outStr = boost::python::extract<std::string>(o);
    	showvar(outStr);
    }
    catch (...)
    {
    	PyErr_Print();
    }
    the error I keep getting is this:
    Code:
    ImportError: __import__ not found
    I may be way off here, but there are literally no examples out there of what I want to do, so I'm really confused, and running out of patience. I hope someone has experience with this and can help me. I don't really know what else to try.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,349
    does anyone have any ideas on this?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,349
    I eventually figured it out with help from this forum post

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a python script and obtaining a return value
    By bhdavis1978 in forum C Programming
    Replies: 3
    Last Post: 09-22-2010, 09:08 AM
  2. Getting boost.python to work
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2008, 06:54 PM
  3. Python Embedding Help...
    By Rendered in forum C Programming
    Replies: 2
    Last Post: 11-17-2007, 09:08 AM
  4. Embedding Python - Link error
    By cboard_member in forum C++ Programming
    Replies: 0
    Last Post: 06-09-2006, 10:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21