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.

Software requirements

– 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:

Build QuantLib for Python

Download QuantLib with Git

This is what you should have in C:\Quantlib:


repoQL

 

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:

C:\boost_1_55_0\bootstrap.bat

  • 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

 

Compiling QuantLib

  • Load the QuantLib solution in VS2008 and in the Options\VC++ Directories, include:
Build QuantLib Python

VS2008 include properties

  • For the lib files:
Compile QuantLib for Python

VS2008 lib directory

Once done you can start building the QuantLib project:

BuildQL2008

 

After the compilation you obtain this lib file:

Build QuantLib for Python

 

Build QuantLib for Python (SWIG)

  •  Set the environment variables LIB for Boost and QuantLib:
Build QuantLib for Python

QuantLib and Boost environment variables

  • Set the environment variable QL_DIR:

C:\Quantlib\QuantLib-SWIG\Python>set QL_DIR=C:\Quantlib\QuantLib

  • Set the environment variable INCLUDE for Boost:

C:\Quantlib\QuantLib-SWIG\Python>set INCLUDE=C:Boost\include\boost-1_55

  • Wrap
Build QuantLib for Python

Wrap command

  • Build the Python QuantLib wrapper using the command:

C:\Quantlib\QuantLib-SWIG\Python>python setup.py build –compiler=msvc

Build QuantLib for Python

Build QuantLib Python Wrapper

  • Install QuantLib Python package to your Python distribution:

C:\Quantlib\QuantLib-SWIG\Python>python setup.py install –skip-build

Build QuantLib for Python

Install QuantLib Package

Quantlib gets installed here:

C:\Python2.7\App\Lib\site-packages\QuantLib

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.

Facebooktwittergoogle_plusredditlinkedinmail