• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

boost python

Joined
5/1/08
Messages
137
Points
26
Has anyone worked with Boost Python? I use boost. I use Python. But I cannot seem to get the boost/Python lib to work ( or even compile ). For anyone who knows, is there anything above and beyond what comes with the package documentation that has to be done? I am finding the documentation to be a bit murky, so if perhaps anyone can explain it in layman's terms, I would greatly appreciate it.

Thanks in advance.
 
Roger, I haven't used it but, did you try the Boost.Python tutorial (http://www.boost.org/doc/libs/1_39_0/libs/python/doc/tutorial/doc/html/index.html)?

Boost.Python documentation is indeed murky but let's start from the bottom.

Yes, this is where I'm getting stuck ( at the beginning ). Trying to build the example module, I'm following the initial steps as follows:

1. build bjam - the executable is placed in C:\Program Files\Boost\boost_1_37_0\tools\jam\src\bin.ntx86. This seems to work fine.
2. go to the example directory: ...\boost_1_37_0\libs\python\example\tutorial
The hello.py, hello.cpp and jamroot files are all there.
3. in the command window call: bjam
I get what appears to be an argument error at bjam attempts to run. No dll is created.

I'm guessing that something is configured wrong, but I have followed the documentation. So, I'm not really sure where to go from here.
 
I have 1_35 version of Boost which contains no python example. I'm downloading the latest version 1_39 right now and try to see if works.
By the way, have you tried to make sure your Boost installation works properly?
https://www.quantnet.com/threads/tutorial-boost-1-37-0-quantlib-0-9-6-and-visual-studio-2008.1084/

I'm pretty sure that boost was installed correctly. I followed those instructions on that thread without issues. In following the build.boost link on the boost.python tutorial page I seem to get something to work. From the ...boost/libs/python/example/quickstart directory I invoked bjam as that example stated: bjam -toolset=msvc --verbose-test test

That seems to have built, and when I run the script.py file in the quickstart directory it runs. However, I try to repeat the above in the python/example/tutorial directory, and I get no love from bjam.

Really starting to wonder if writing C-extentions in boost/python IS as easy as python itself. Enough for now - I'm going to import sleep.

Ideas welcome :wall
 
You have 1.37, why not get 1.39 and give it a try again.
From the boost.python page, it clearly says that the examples in /quickstart does not require Boost, hence No-Install Quickstart. That's how I understand it.
So you can't get other .py to run means you didn't get the boost.python to build correctly.
My boost is still building so it will take a while before I get to test it. I'm reading the page and it looks like the build and config steps are not that simple. Did you get to step 4, 5?
 
I'm pretty sure that boost was installed correctly. I followed those instructions on that thread without issues.
When I followed the instruction to build Boost from that QN thread, I noticed that it has this message a few minutes in
C++:
Skipping build of:  libs/python/build/boost_python  <build>no in common properties
Not sure you got the same message but it may have something to do with it. That may also mean there are extra steps to build the python.
 
When I followed the instruction to build Boost from that QN thread, I noticed that it has this message a few minutes in
C++:
Skipping build of:  libs/python/build/boost_python  <build>no in common properties
Not sure you got the same message but it may have something to do with it. That may also mean there are extra steps to build the python.

yup. I'm thinking the extra step requires telling bjam about where to find python. But I've tried several different combinations of build commands ( as referenced in boost documentation ) without success. I don't know, but I think Kim Jong-Il is behind all of this.
 
You might need to talk to Chuck Norris solve this issue ;)
 
You might need to talk to Chuck Norris solve this issue ;)

I already did. Let's just say I found a chink in the armor.


Edit: Alain, perhaps you should make this a bonus task for your C++ refresher. There's got to be someone out there who can do what Chuck Norris can't. Now, I'm probably going to be stuck by lightning.
 
This is one example of a thread that was in need of some closure. After some time, I think I have figured it out. For anyone interested in doing something computationally intensive, but afraid to use python because it would be too slow, this may prove to be a good solution.
I understand that linux users probably just have to snap their fingers to get boost.python to work, but it was proving difficult under windows. Since I still feel some loyalty to windows, and I can't snap my fingers very well, I wanted to see if I could get it to finally work here.

Below are the steps that seem to make it work:

1. Boost is now up to 1.41, but it can still be built per the instructions here:
TUTORIAL: Boost 1.37.0 + QuantLib 0.9.6 and Visual Studio 2008

2. The build should create a few different boost.python dlls.

3. This step is just for organization. Create a folder and place the dlls from step 2 there. Add this folder to the PATH and PYTHONPATH. If you don't want to create this folder, I think you could probably just add C:\boost\libs to the path. The important thing is that python and the extensions you create down the road can see these libraries.

4. Create your extension as per the guidelines here:
Chapter*1.*python 2.0
The boost python library basically uses a macro to wrap regular c++ code into a python module. So, you can use previously written code or libraries, and just wrap them up ( no need to change anything you might have written before ).

5. I use MSVC9.0, but whichever tool set you use, just make sure that your project builds to a shared library (dll), but change the output file to be a .pyd, which is just a renamed dll. All newer versions of python look for pyd instead of dll extensions. If you created the file from step 3, put this pyd file in there. If not, then put this library somewhere in your path.

7. Invoke python and run like you normally would. For example, I created the extension "boosthello_ext". The session goes as follows:
C++:
>>>import boosthello_ext
>>>print boosthello_ext.greet()
-> hello, boost
>>>

6. And Bob's your uncle. There is a remarkable increase in performance for intensive computations. I added a function to the above module that just loops over an inputted number. The results are below; run is the iteration number, num is the number of loops in the iteration, python is how long it takes the interpreter to run, ext is how long it takes the boosthello module to run:
C++:
run 1; num: 1000000, python: 0.128761, ext: 0.001314
run 2; num: 2000000, python: 0.263353, ext: 0.002688
run 3; num: 3000000, python: 0.391827, ext: 0.003960
run 4; num: 4000000, python: 0.533130, ext: 0.005340
run 5; num: 5000000, python: 0.653911, ext: 0.006662
run 6; num: 6000000, python: 0.789581, ext: 0.008092
run 7; num: 7000000, python: 0.921839, ext: 0.009255
run 8; num: 8000000, python: 1.049060, ext: 0.010754
run 9; num: 9000000, python: 1.194949, ext: 0.012029
run 10; num: 10000000, python: 1.331470, ext: 0.013288
For reference, the helloboost code is below:
C++:
# include <boost/python.hpp>
 
char const* greet()
{
    return "hello, boost";
}
 
double simulate( int num )
{   
    double sum = 0;
    for ( int i = 0; i < num; i++ )
        sum += i;
    return sum / num;
}
 
BOOST_PYTHON_MODULE(boosthello_ext)
{
    using namespace boost::python;
    def("greet", greet);
    def("sim", simulate);
}
 
Back
Top