Skip to content

Homebrew and Python

justinmayer edited this page Oct 27, 2012 · 41 revisions

Overview

Homebrew provides a formula for Python 2.7.x. This is somewhat counter to the "no system duplicates" policy, but Leopard comes with 2.5.x, Snow Leopard comes with 2.6.x, and it is useful to be able to get the latest Python 2 in an easy way.

Issues

Assuming a standard Homebrew install, the Prefix will be /usr/local and the Cellar will be /usr/local/Cellar.

Homebrew installs Python to the Cellar, using the standard ./configure --prefix=#{prefix}.

This sets up:

  • the "site-packages" folder as /usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages.
  • the "install-scripts" folder as /usr/local/Cellar/python/2.7.3/bin.

If site-packages lives in Python's Cellar, then user installed packages will be wiped between Python updates. Same for install-scripts, with the added problem that users have to manually add Python's cellar bin folder to the path.

Solution

Homebrew performs two actions on install to address these issues.

First, the Cellar site-packages folder is removed, and a symlink to /usr/local/lib/python2.7/site-packages in the Prefix is created. This will allow site-packages to persist between Python updates, as Homebrew has special handling for some languages that use lib for user-installable libraries.

Second, a distutils.cfg file is written to set the install-scripts folder to /usr/local/share/python. Users can add /usr/local/share/python to the PATH to pick up installed scripts.

Note: you need to prepend /usr/local/share/python to PATH otherwise you'll still pick-up OS X's easy_install. Thus you can't set it through ~/.MacOSX/environment.plist as that will always include /usr/bin first. You need to set it either per user (~/.profile / ~/.bashrc) or systemwide by editing /etc/paths and adding /usr/local/share/python to the top of that file (the latter is not recommended as that overrides OS X defaults on a global level, which might break stuff).

These changes allow Homebrew to play along with distutils (which is the basis for distribute and pip).

Standard python installers can be run as python setup.py install and the proper paths will be selected.

Distribute, Pip, etc.

The Python formula installs Pip and Distribute, the latter of which provides easy_install.

Distribute can be updated via Pip, without having to re-brew Python:

pip install --upgrade distribute

Similarly, Pip can be used to upgrade itself via:

pip install --upgrade pip

The separate distribute and pip formulae have been removed.

Clone this wiki locally