Setting up a Python Scientific Environment (NumPy, SciPy, pandas, StatsModels, etc.) in OS X 10.9 Mavericks

It is better than a nightmare from which you cannot wake up …

  1. Install Homebrew:

    $ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
    
  2. Find problems and fix them (typically resulting from Homebrew becoming very stroppy if it does not have exclusive access to “/usr/local“):

    $ brew doctor
    

    VERY IMPORTANT NOTE: Make ABSOLUTELY sure that the DYLD_LIBRARY_PATH environmental variable is NOT set. Having this set will cause all sorts of problems as conflicts arise between libraries that homebrew installs that some of your other packages need (e.g., "libpng" and MacVim, respectively). Worse, some builds get confused and fail (e.g., “matplotlib“). What happens if you need DYLD_LIBRARY_PATH for other applications? Please direct your questions to the homebrew folks.

  3. Install Homebrew’s Pythons:

    $ brew install python
    $ brew install python3
    
  4. Either create and source a virtual environment using Homebrew’s Python:

    $ virtualenv -p /usr/local/python3 homebrew-stats-computing
    $ . homebrew-stats-computing/bin/activate
    

    Or make sure Homebrew’s Python is at the head of the “$PATH“:

    $ export PATH=/usr/local/bin:$PATH
    
  5. Install The NumPy + SciPy + pandas stack:

    $ brew install gfortran
    $ pip3 install numpy
    $ pip3 install scipy
    $ pip3 install pandas
    
  6. Install StatsModels:

    $ pip3 install cython
    $ pip3 install patsy
    $ cd /tmp
    $ git clone https://github.com/statsmodels/statsmodels.git
    $ cd statsmodels
    $ python3 setup.py build
    $ python3 setup.py install
    
  7. Install matplotlib:

    $ brew install libpng freetype pkgconfig
    $ cd /tmp
    $ git clone https://github.com/matplotlib/matplotlib.git
    $ cd matplotlib
    $ python3 setup.py install
    

Leave a Reply

Your email address will not be published. Required fields are marked *