I wanted to write this article to share my experience of Quantlib compilation for Python. And also it recapitulates some stages that I could forget if i had to uninstall a Python distribution and if i therefore had to build again QuantLib for Python and re-install its package.
Also for someone who never followed this process, the next lines are a step by step guide to build QuantLib for Python. That’s good to know before running any Python script calling QuantLib.
Some built packages (like here) can be found on internet but I wanted to be able to run the Trunk version of Quantlib and expose it in Python. Have a grasp on the version used.
– A python distribution (ex: 2.7, 3.x)
– For QuantLib compilation you need a Visual Studio 2008 (if you have a Python 2.7 distribution), or Visual Studio 2010 for Python 3.x.
Personally i compiled QuantLib with Visual Studio 2008 since i had a 2.7.6 Python Portable distribution. That’s the configuration i used in the following lines.
Getting Quantlib with Git
First make sure that you have installed Git: http://git-scm.com/downloads
Then once it’s done, run the following command:
This is what you should have in C:\Quantlib:
Get and install Boost
- Download boost sources, and unzip them (say in C:\ folder).
- Enter the following command line (if you have boost 1.55) to build the Boost.build tool b2.exe:
- Then build boost with this command:
C:\boost_1_55_0\b2 toolset=msvc-9.0 –build-type=complete –abbreviate-paths architecture=x86 address-model=32 install
The compilation takes place and Boost willthen be installed in C:\Boost
- Load the QuantLib solution in VS2008 and in the Options\VC++ Directories, include:
- For the lib files:
Once done you can start building the QuantLib project:
After the compilation you obtain this lib file:
Build QuantLib for Python (SWIG)
- Set the environment variables LIB for Boost and QuantLib:
- Set the environment variable QL_DIR:
- Set the environment variable INCLUDE for Boost:
- Build the Python QuantLib wrapper using the command:
C:\Quantlib\QuantLib-SWIG\Python>python setup.py build –compiler=msvc
- Install QuantLib Python package to your Python distribution:
C:\Quantlib\QuantLib-SWIG\Python>python setup.py install –skip-build
Quantlib gets installed here:
You can run the test command to run QuantLib tests:
C:\Quantlib\QuantLib-SWIG\Python>python setup.py test
Now you should have a QuantLib-Python package up and running within your list of Python packages.
The next article will illustrate how you can call QuantLib in a Python script, now that you have installed this Python wrapper.