Skip to main content
gamedev.net
🔒 Locked

Problems with boost.python

Started by h3ro Dec 16, 2008 at 8:37 PM 12 replies 4.3k views
Original Post
h3ro
h3ro
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?
outRider
outRider
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.
Jroggy
Jroggy
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.
mrbastard
mrbastard
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"]
h3ro
h3ro
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?
Kylotan
Kylotan
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.)
h3ro
h3ro
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.
visitor
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 :))
the_edd
the_edd
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.
Kylotan
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.)

Jroggy
Jroggy
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.

h3ro
h3ro
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?
Kylotan
Kylotan
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.
the_edd
the_edd
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.