QuantLib-Python installation on Mac OS X
Installation from PyPI (recommended)
If you don't need to modify the wrappers, you might want to try installing a precompiled binary version. The availability of binaries depend on your operating system; to try to install them, run:
python -m pip install QuantLib
(If you have multiple versions of Python installed, run the above with the one you want to use QuantLib with.) If a binary package is available for your system, it will be installed and you will be able to leave this page and use it right away; if not, you'll have to compile it yourself as described in the next section.
Installation from a released version
The following assumes that you already installed QuantLib. Instructions for that are available at http://quantlib.org/install/macosx.shtml. In particular, check that you have provided the required options and environment variables to ./configure and that you can execute the quantlib-config script.
You can download released QuantLib-SWIG versions from GitHub at https://github.com/lballabio/QuantLib-SWIG/releases.
Once you have the tarball, extract it by executing:
tar xzf QuantLib-SWIG-1.29.tar.gz
(1.29 is the most recent version at the time of this writing; you
might have downloaded another one, but take care to use one compatible
with the version of QuantLib you installed.) This creates a
folder QuantLib-SWIG-1.29
; enter it and build QuantLib by
executing the following commands. For QuantLib 1.22 only, you will
also need to add -std=c++11 to CXXFLAGS. Again,
make sure that you can execute quantlib-config from the
terminal, since the setup.py build will call it to retrieve a
few required compilation flags.
On Mac OS X 10.11 (El Capitan) and later,
cd QuantLib-SWIG-1.29/Python export CXXFLAGS='-O2 -stdlib=libc++ -mmacosx-version-min=10.9' export LDFLAGS='-stdlib=libc++ -mmacosx-version-min=10.9' python3 setup.py build
On Mac OS X 10.9 (Mavericks) and 10.10 (Yosemite),
cd QuantLib-SWIG-1.29/Python export CXXFLAGS='-O2 -stdlib=libstdc++ -mmacosx-version-min=10.6' export LDFLAGS='-stdlib=libstdc++ -mmacosx-version-min=10.6' python3 setup.py build
On earlier systems,
cd QuantLib-SWIG-1.29/Python export CXXFLAGS='-O2' python3 setup.py build
Contrary to popular belief, working from a released tarball doesn't require you to have SWIG installed. After building, you can run
python3 setup.py installto perform a system installation; however, you might consider either passing an installation path, as in:
python3 setup.py install --prefix=/your/desired/locationor building a wheel with
python3 setup.py bdist_wheeland use it for installation in the location you want; this will require the wheel package installed.
Also, note that simply calling
python3
as written above will find the first version of
Python in your path. If you want to use a different one (for instance
because you installed multiple versions of Python, or you want to use
an Anaconda installation) you must call your chosen Python
interpreter; for instance, if you have Python 3.9 installed
as /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
,
you'll have to run:
CXXFLAGS='...' LDFLAGS='...' /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 setup.py build
Once you're done, you can try to run a few examples to check your installation. To do this, you can execute:
python3 setup.py test
Installation from a git repository
If you want to compile from a checkout of a git repository (such as the official one at https://github.com/lballabio/quantlib-swig, or a fork of it that you might have created) you'll need SWIG available; you can download and install it from http://swig.org or, again, get it packaged from Homebrew or MacPorts.