Saturday, January 26, 2013

Running c++ code from python using Boost.Python

I am currently reading a book "Production Volume Rendering". The book images were the main reason I started reading it. However, since the content of the book is written into a library"PVR" which is written in C++ but it is controlled using Python scripts. Thus, to get started, I needed to understand how the whole thing is chained from the c++ code to the python scripts. So I started to get my c++ code to work from a Python script so here it is. This is what I learned. I used VisualStudio 2010 and python27.

In visualstudio 2010, create a new empty win32 dll project named "hello". Next create an empty c++ file. The contents of the file are as follows:

#include < iostream >
using namespace std; 
void say_hello(const char* name) { 
   cout << "Hello " <<  name << "\n"; 


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


using namespace boost::python; 

BOOST_PYTHON_MODULE(hello) { 
   def("say_hello", say_hello); 


Nothing fancy here. I simply create a function that says hello world on screen usign standard IO routine. 

In order to have my function exported properly I added a module definition file (.def) to the project. This file contains: 
LIBRARY hello 
EXPORTS say_hello 

This tells the compiler to add the say_hello function to dll. 
Next, I compiled and build the dll if all goes well you have the dll created.

Now on the python side. I copied my hello.dll file to an empty directory. This directory contained another file i.e. my python script (hello.py). The python script contains:
import hello 
hello.say_hello("World")

I tried to run the python script using python hello.py 
This returned the following error 
d:\TestHello > python hello.py 
Traceback (most recent call last): 
File "hello.py", line 1, in import hello 
ImportError: No module named hello 

Now searching online I found that for my dll to be recognized by Python, it needs to be renamed to .pyd extension. So i changed the extension to .pyd and ran the python script again. This time, it gave me another error which was 
d:\TestHello > python hello.py 
Traceback (most recent call last): File "hello.py", line 1, in import hello 
ImportError: DLL load failed: The specified module could not be found. 

Now once again I went online to find the reason for this error. Fortunately, on one of the websites, I found the reason for this error. Basically, the Python interpreter is unable to load a dependency dll while loading my hello.pyd (dll). Which dll is it? This I came to know by opening my hello.pyd(dll) in the dependency walker (http://www.dependencywalker.com/). This showed me that I was missing the boost_python-vc100-mt-gd-1_51.dll which is needed. So i copied this dll to the same place as my hello.pyd (dll). 

Now running the python script again ran the code fine. 
d:\TestHello > python hello.py 
Hello World! 

Some notes to take away: 
1) Rename your c++ dll to .pyd otherwise Python will not load it. 
2) Name the module name the same as your dll name i.e. BOOST_PYTHON_MODULE(< your dll name >) 
3) Make sure that all of the dependency dlls are in the same folder or in the path. 

Hope this helps others. I went through a lot of pain with this one. But finally I got it working.

16 comments:

sharrajesh said...

Thank you. It was helpful.

MMMovania said...

Glad it has helped u too.
Thanks,
Mobeen

Anonymous said...

Excellent tips! I've been battling with VS for a few hours on this. Thanks!

MMMovania said...

You are welcome. Glad it helped you too ;).

metal4people said...

Thanks, you have helped me a lot!

MMMovania said...

You are always welcome. Glad it helped you too.

Unknown said...

After hours of Blockage, Frustration, Irretation finaly Ur Trick helped me to use Boost Library. Kudos.

MMMovania said...

I am glad it helped.

Unknown said...

I ran into the same problems, very helpful! Thank you very much!

MMMovania said...

Glad it helped you Fabian.

Anonymous said...

where does the dll file gets created? I mean the location path.

MMMovania said...

That depends on which IDE you are using. For visualstudio it stores the dll in the Debug/Release sub-folder of the current solution folder.

Unknown said...

You are a lifesaver. I've been looking for this information for several days, and renaming my dll to `hello.pyd` instead of `hello.dll` was the crucial step I was missing. Thanks a ton!

MMMovania said...

You are welcome Jimmy. Glad it was helpful.

Unknown said...

Thank you ...
Copy/pasting the *.dll made me save hours ... After having already lost a bit of them !

Have a nice day

MMMovania said...

HI Pierre,
I am glad it helped you too.

Regards,
Mobeen

Popular Posts

Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. theme. Powered by Blogger.