Problems with boost.python

Started by
12 comments, last by the_edd 15 years, 4 months ago
I am working on making an easier way to add new things to my program, so I decided to create support for python scripting. Getting python to work turned out to be a lot more work then I initially thought. I have installed python and built the needed boost libraries and compiled the example, but I cant get the example to run Here is the code I use to create the .dll file

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

The dll file is named hallo_ext.dll and is placed in the same directory as the python file. Here is the code in the python file:

import hello_ext

print hello_ext.greet()
raw_input("Press return to close this window...")


Error outputted from the python file: ImportError: No module named hello_ext. Does anyone know what I am doing wrong?
Advertisement
Your DLL is called hallo_ext.dll but you're trying to import hello_ext? If that was just a typo I would check that Python's module search path includes the current directory. Try print(sys.path) in your script somewhere.
rename hello_ext.dll to hello_ext.pyd Should solve your problem :) Also make sure the dll is either in site-packages or the same directory as your script.
you should also ensure the name you give the module in the BOOST_PYTHON_MODULE declaration is the same as the pyd file.
[size="1"]
I changed the name of the dll to pyd, but now I get a different error.
The dll is in the same folder as the script.

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

I have never written a python script before, so it might be something very basic that is wrong. Any ideas?
Did you fix the hallo/hello mismatch? (If it even existed at all, that is - be sure you type in here exactly what you see.)
The hallo/hello typo is only in the post. It was a typo when I wrote it up, the files are named correctly.

All the code is from the example that comes with boost, so I cant understand why it is not working.
Not that I have done this myself, but did you follow the steps on the next page of that tutorial. I would somehow expect that you get both a binary dll and a python module since it would be strange for python to understand a binary file... (but I'm a total noob in this and might try it if you work it out :))
There have been a couple of recent releases for Python so it may be the case that the C API has changed. Which version of Python are you using and are you sure Boost.Python generates DLLs with the correct function signatures?

This is a guess, but it's something you should be sure of anyway.
Quote:Original post by the_edd
There have been a couple of recent releases for Python so it may be the case that the C API has changed.

Yep, the Python C API changes for every minor release (eg. 2.5 to 2.6). This means that native-code compiled extensions have to be built individually against each version. h3ro has to make sure that the python.h on his compiler's path is for the same version of Python that is trying to import the module. (If there is only 1 Python installation, obviously this won't be the problem.)

This topic is closed to new replies.

Advertisement