# Quantlib with python on mac?

Is there anyone who knows a good guide to get quantlib working for python om mac? I have tried to search online and have not found any good guidance. I need to use quantlib for a project on python.

It seems that there is no quantlib for python for mac available.

Thanks.

• I'm sure I've seen Luigi Ballabio presented his Quantlib on the Python notebook. Can you do a search on it? – SmallChess Feb 24 '16 at 23:27

The installation process should be the same as on Linux. Once you have the C++ QuantLib library installed (instructions for that are on the QuantLib site, at http://quantlib.org/install/macosx.shtml) you can download the latest QuantLib-SWIG release, uncompress it and run:

./configure
make -C Python
sudo make -C Python install


Note that the above work with a release (as in, the files you get from https://sourceforge.net/projects/quantlib/files/) and contrary to popular belief, don't require you to have SWIG installed.

Instead, if you want to work from a checkout of the git repository (either because you want to modify the interfaces, or because you want a new feature which is not yet available in a release) you'll need SWIG—but not a separate command, as make takes care of it—and you'll also need to run

./autogen.sh


before the other commands; this will require automake and autoconf to be installed. (End of note.)

There are two or three caveats I can think of. The first is that the ./configure step will need to invoke quantlib-config to find out what flags should be passed to the compiler and linker (they will also include the additional include directories you might have specified when you built QuantLib, so you'll be covered even if you have, say, Boost in a non-standard place). This means that quantlib-config must be in your path.

The second is that, unfortunately, at this time make ignores any prefix you might pass to ./configure and always installs to the default location, which requires you to use sudo as I wrote above. If you don't have admin rights, you'll have to look into the Makefile and pass your prefix to setup.py.

The third is that you'll need the Python development files (python.h and so on). They might be available by default (and if they are, make calls setup.py which should find them for you); but if not, you'll have to figure out how to get them.

Finally, if you use Docker, you can pull an Ubuntu image with an installation of quantlib-python from https://hub.docker.com/r/lballabio/quantlib-python/.

• Hi, I tried the method using the latest QuantLib-SWIG release, using the commands u gave me. However when trying to use import QuantLib as ql in python, it does not work. It still says "No module named QuantLib" – Elekko Feb 25 '16 at 17:34
• Where did the last command install the module? (It should output the info on screen.) You must probably make sure that the location is in your PYTHONPATH. – Luigi Ballabio Feb 25 '16 at 17:41
• Actually I have problems with make -C Python, it gives an long error: clang: error: no such file or directory: 'QuantLib/quantlib_wrap.cpp' clang: error: no input files error: command 'cc' failed with exit status 1 make[1]: *** [.build-stamp] Error 1 make: *** [install] Error 2, – Elekko Feb 25 '16 at 17:52
• Any chance you can paste it somewhere on the net and post a link? Does it say anything about a header or library not found? – Luigi Ballabio Feb 25 '16 at 17:55
• here you go: pastebin.com/cztK0GW8 – Elekko Feb 25 '16 at 17:58

Probably the easiest way to get it is through MacPorts, which will take care of the Python dependencies, et cetera, for you. If you have not already done so, you can get started with MacPorts using these instructions.

After MacPorts is installed, you can simply invoke

sudo port install QuantLib


which pretty much just follows the official recommendations from the QuantLib people with the right library pointers.

• This only installs the C++ library, though. It doesn't look like the Python bindings are included. – Luigi Ballabio Feb 25 '16 at 22:55
• Thanks for the clarification...I guess I had only tried it from R. Is there a good way to add the linkings to the MacPorts Python? – Brian B Feb 26 '16 at 13:22
• I wouldn't know. I'm not familiar with MacPorts. – Luigi Ballabio Feb 26 '16 at 14:14

just as a note, I compiled for QuantLib 1.9 and Python 2.7.12. I installed QuantLib using macports, then I spent a lot of time trying to understand the compilation error. Do as follows:

1) Ignore the ./configure and make steps 2) Go straight for the Python folder 3) run: python setup.py build 4) after complaining about the i386 crap and x86_64, run the following (compile again):

g++ -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I/opt/local/include -I/opt/local/include -c QuantLib/quantlib_wrap.cpp -o build/temp.macosx-10.6-intel-2.7/QuantLib/quantlib_wrap.o -Wno-unused

g++ -bundle -undefined dynamic_lookup -arch x86_64 -g build/temp.macosx-10.6-intel-2.7/QuantLib/quantlib_wrap.o -L/opt/local/lib -L/opt/local/lib -lQuantLib -o build/lib.macosx-10.6-intel-2.7/QuantLib/_QuantLib.so

6) DON'T USE python setup.py install

7) Run python setup.py test, check if it works

8) copy the QuantLib folder in build/lib.macosx-10.6-intel-2.7/ to python site-packages (Frameworks/Python.framework/Versions/2.7/lib/Python2.7/site-packages

Test it in the interpreter. Things that others can pursuit:

Honestly, I don't know the reason for the two arch flags in the build process (-arch i386 -arch x86_64), but I know that this is the problem for linking the libs. I checked everything in the setup.py and the environment variables, I don't know where it is set. Also, I don't know why the sudo python setup.py install fails to move properly the files.

I believe Enthought has Python wrappers that are limited in scope. Ensure you have QuantLib and its dependencies installed:

git clone https://github.com/enthought/pyql.git
cd pyql
python setup.py build
python setup.py test
python setup.py install


Otherwise, the canonical way is to use the SWIG bindings for OS X.

• I know the Enthought library, but it's too limited. It's more like a prototype. – SmallChess Feb 25 '16 at 4:44