2
$\begingroup$

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.

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

4 Answers 4

4
$\begingroup$

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/.

$\endgroup$
8
  • $\begingroup$ 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" $\endgroup$
    – Elekko
    Feb 25, 2016 at 17:34
  • $\begingroup$ 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. $\endgroup$ Feb 25, 2016 at 17:41
  • $\begingroup$ 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, $\endgroup$
    – Elekko
    Feb 25, 2016 at 17:52
  • $\begingroup$ 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? $\endgroup$ Feb 25, 2016 at 17:55
  • $\begingroup$ here you go: pastebin.com/cztK0GW8 $\endgroup$
    – Elekko
    Feb 25, 2016 at 17:58
1
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ This only installs the C++ library, though. It doesn't look like the Python bindings are included. $\endgroup$ Feb 25, 2016 at 22:55
  • $\begingroup$ 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? $\endgroup$
    – Brian B
    Feb 26, 2016 at 13:22
  • $\begingroup$ I wouldn't know. I'm not familiar with MacPorts. $\endgroup$ Feb 26, 2016 at 14:14
1
$\begingroup$

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

5) Link again:

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.

$\endgroup$
0
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ I know the Enthought library, but it's too limited. It's more like a prototype. $\endgroup$
    – SmallChess
    Feb 25, 2016 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.