3

I have written simple HelloWorld class. and write a boost python wrapper.and debug the code as DLL.My question is how can i expose this code in python and use greet function.I tried by giving the path in sys.path.insert. but not able to get greet function. The code i treid is below. Thanks for help.

#include<boost/python.hpp>

using namespace std;
using namespace boost::python;
class World
{
 public:
 string msg;
 void set(string msg)
{
    this->msg=msg;
}

 string greet()
 {
   return msg;
 }

};

BOOST_PYTHON_MODULE(ExpsoingClasses)
{
class_<World>("World")
    .def("greet", &World::greet)
    .def("set", &World::set)

    ;
}
user2696156
  • 337
  • 1
  • 3
  • 12

1 Answers1

3

At least on my system, I had to rename the library file from ExpsoingClasses.dll to ExpsoingClasses.pyd before I could import it in Python. Once you do that, this should work:

import ExpsoingClasses
retVal = ExpsoingClasses.World()
retVal.set('hello world')
print retVal.greet()
A_K
  • 457
  • 3
  • 11
  • thanks for the help. I build as a debug version and which path of file i have to give in sys.path,insert. I wrote sys.path.insert(0,"C:\Documents and Settings\X157574\Desktop\Kashif\Boost Python Examples\Exposing Classes\debug"). but its not working. and i also tried rename the dll to .pyd but its not working – user2696156 Sep 09 '13 at 08:08
  • I don't think Python likes paths in that format. You want pairs of forward slashes rather than backslashes. Try this: sys.path.insert(0,"C://Documents and Settings//X157574//Desktop//Kashif//Boost Python Examples//Exposing Classes//debug") Also, you can seperate the issue of "is my .pyd functional" from the issue of "can python find my .pyd" by copying the .pyd to the same folder as the python script you're writing. – A_K Sep 09 '13 at 13:10
  • thanks for the help. now i am able to import ExposingClasses. but still its greet and set function is not accessable – user2696156 Sep 09 '13 at 14:47
  • The only thing that comes to mind is the spelling of "ExposingClasses" in your code; it says "ExpsoingClasses". Make sure you use the same spelling everywhere. – A_K Sep 09 '13 at 19:47
  • Thanks. I Correct the mistake.but still i cannot access World Class and set and greet fucntions.I am using python 3.Is there any other way to access these method? – user2696156 Sep 10 '13 at 09:05
  • Well, I don't see anything else wrong with your C++ code from your question, but upload your Python code somewhere and I will take a look at it. – A_K Sep 10 '13 at 12:42
  • Thanks . Code is debugging but its not importing in python. i changed the dll file into pyd. can you please tell how to give the exact path of cpp file which i want to import . – user2696156 Sep 12 '13 at 14:24
  • In my code, I have "sys.path.append('.//bin//release//')" followed by "import libCommunicator" where libCommunicator.pyd is inside folder bin\release relative to the directory of the Python script importing it. – A_K Sep 13 '13 at 17:21
  • still not able to import it from python. when i change ExpsoingClasses.dll to ExpsoingClasses.pyd. the error is "ImportError: DLL load failed: The specified module could not be found." – user2696156 Sep 16 '13 at 12:56