0

I am trying to reproduce the Boost.Python tutorial on how to do a wrap a C/C++ file for python.

This is the cpp file which builded successfuly.

#include "boost/python.hpp"
class SaySomething
{
public:
    void set(std::string msg)
    {
        this->msg = msg;
    }
    std::string greet()
    {
        return msg;
    }
    std::string msg;
};
BOOST_PYTHON_MODULE(SaySomething)
{
    namespace python = boost::python;
    python::class_<SaySomething>("SaySomething")
        .def("greet", &SaySomething::greet)
        .def("set", &SaySomething::set)
    ;
}

In the same directory of the source code I have the python file

import SaySomething


msg = SaySomething.SaySomething()
msg.set('howdy')
msg.greet()

When I try to run the python file this error occurs

ModuleNotFoundError: No module named 'SaySomething'

I tried moving the compiled library to the same folder with no success. The tutorial doesn't elaborate on how to make the C++ file seen by the interpreter.

Edit

As some have suggested I have edited the extension of the compiled shared library to .pyd so python can see it. This creates a second error

ImportError: DLL load failed: The specified module could not be found.

This issue is exactly like mine. The solution suggests to add Boost.python to environnement variable. I know how to add path to this but I'm not sure what path I want, the boost.python dlls (or lib)? Just boost?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hawoona
  • 31
  • 7
  • 1
    Does this answer your question? [C++ Boost.Python : 2 problems](https://stackoverflow.com/questions/9140572/c-boost-python-2-problems) – Alan Birtles Jan 10 '20 at 23:01
  • Not exactly. I made sure that the file, the library and the module is named after the same thing. My library is SaySomething.dll and the module in BOOST_PYTHON_MODULE is SaySomething as well. – Hawoona Jan 11 '20 at 17:13
  • Could it have anything to do with the extension of the shared library? Using windows it's .dll but the similar issues I've read use .so (linux). Is python only looking for *.so files? – Hawoona Jan 11 '20 at 17:31
  • 1
    You need to change the extension of the DLL to `pyd`. – Dan Mašek Jan 12 '20 at 01:59
  • I'm gonna look into that when I get back. Can I just edit the extension or do I have to make a separte build with microsoft visual studio? – Hawoona Jan 12 '20 at 06:29
  • 1
    Simple rename will do. – Dan Mašek Jan 12 '20 at 13:41
  • 1
    How are you building it? Are you following [the tutorial exactly](https://www.boost.org/doc/libs/1_67_0/libs/python/doc/html/tutorial/tutorial/hello.html) or are you doing something different? – DavidW Jan 13 '20 at 21:13
  • I builded it using .\b2, so I did not use Bjam. More accurately, I'm following the tutorial but did not download any of their files. I builded boost.python with `.\b2`, made my own solution SaySomething and created my own python file. – Hawoona Jan 13 '20 at 21:30
  • 1
    @Hawoona OK, well given that it's quite likely a build problem I suggest you edit the exact details of how you built it into the question so that someone else can reproduce what you've done – DavidW Jan 14 '20 at 12:43
  • You need to edit your environmental variable PATH to include the path to your boost python dll that you built with b2. – stackoverblown May 14 '20 at 18:47

0 Answers0