Sign in to follow this  
h3ro

Problems with boost.python

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :))

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
Quote:
Original post by visitor
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 :))

afaik a pyd is just a renamed dll.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
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.)


I only have one version installed, version 2.6

EDIT:
Is there an other wrapper for python which is easier to get to work? Or maybe a different scripting language?

Share this post


Link to post
Share on other sites
I must admit I wrap my Python by hand, but then I am a bit of a masochist. (And also consider Boost::Python very heavyweight and awkward.)

If you try the FAQ for the Scripting Languages and Mods forum, I think I listed some Python wrapper libraries in there. Or Google, using keywords like Python, wrappers, bindings, C++, etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by h3ro
EDIT:
Is there an other wrapper for python which is easier to get to work? Or maybe a different scripting language?


You could look at Lua. There are a bunch of C++ libraries that allow you to easily create a bridge to Lua. My favorite is Luabind, but google around and you'll find a bunch of others, some of which you might personally prefer. The C API is really rather nice anyway, so you could even go without a wrapper if needed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this