MacPorts: The Key to Python Happiness

For those who want the TL;DR version which gives you all the commands you need to copy and paste into a terminal window, then it’s all here.

To do some of the work I do, I needed to have a working version of Python that included the numpy, scipy, and matplotlib libraries. I could not, however, get all these pieces to come together using homebrew. After trying a number of approaches from a variety of sources, I turned to StackOverflow for help. I got a response from tiago, who noted that “Homebrew and pip are great for minimalistic, pure python packages. But they stumble spectacularly with scipy or packages that require external non-python packages.” His advice was to turn, again, to MacPorts. (My first step was to un-install homebrew. After that, it was time to crank up the MacPorts assembly.)

Installing MacPorts

First, before you do anything else, you’ll need to make sure that you have Xcode’s command line tools installed. Installation is now as easy as typing the following in a terminal window:

xcode-select --install

You’ll get a GUI dialogue box, agree to the EULA, and then installation will happen. (And I believe software update / the App store will track updates for you.)

Second, Download the Mac OS X Package pkg Installer and step through the GUI install.

MacPorts should, as part of the install process, run sudo port selfupdate -v but you can always run it again. You know, just to make yourself feel better.

Third, you’ll need to install a version of Python. In my case, I am building a setup around Python 2.7, and so I entered sudo port -v install python27. The -v option gives you a verbose description of what’s happening. Be prepared to watch a lot of stuff scroll by. (If you’d rather not see all that and having the machine quietly do its thing, you can leave the -v off. Good for you for having quiet confidence in your Mac.)

MacPorts gives you some nice functionality with its search feature, which you can use to find MacPort portfiles. In my case, I wanted to start with numpy and so I entered port search numpy and got the following:

py-imread @0.2.5 (python, graphics)
    Reads images into numpy arrays

py-numpy @1.6.2 (python, math)
    The core utilities for the scientific library scipy for Python

py24-numpy @1.6.2 (python, math)
    The core utilities for the scientific library scipy for Python

py25-numpy @1.6.2 (python, math)
    The core utilities for the scientific library scipy for Python

py25-symeig @1.4 (python, science)
    Symeig - Symmetrical eigenvalue routines for NumPy.

py26-imread @0.2.5 (python, graphics)
    Reads images into numpy arrays

py26-numpy @1.6.2 (python, math)
    The core utilities for the scientific library scipy for Python

py26-scikits-audiolab @0.11.0 (python, science, audio)
    Audiolab is a python toolbox to read/write audio files from numpy arrays

py27-imread @0.2.5 (python, graphics)
    Reads images into numpy arrays

py27-numpy @1.6.2 (python, math)
    The core utilities for the scientific library scipy for Python

py31-numpy @1.6.2 (python, math)
    The core utilities for the scientific library scipy for Python

py32-numpy @1.6.2 (python, math)
    The core utilities for the scientific library scipy for Python

py33-numpy @1.6.2 (python, math)
    The core utilities for the scientific library scipy for Python

Found 13 ports.`

That py27-numpy is the one I want, and so I entered sudo port install py27-numpy. More scrolling. Done. Repeat these steps for scipy and matplotlib and nltk.

Finally, a crucial step is to make it so that your setup turns to your nice custom install of Python and not the one that came with the system. I usually accomplish this by editing my .bash_profile, but this did not work for me. Luckily, MacPorts has the solution: sudo port install python_select. Once you’ve done this, enter sudo port select --set python python27 and you’re done.

2 thoughts on “MacPorts: The Key to Python Happiness

  1. » Getting NLTK Up and Running on Mac OS X logbook

  2. Package mgr | Annotary

Comments are closed.