Skip to content

Homebrew and Python

samueljohn edited this page Dec 6, 2012 · 28 revisions

Overview

Python is a powerful interpreted language.

Homebrew is designed to work - or should work - with any CPython, in particular with OS X system's python, but also provides formulae to brew a more up-to-date Python 2.7.x. However, if you choose to use another Python than these two alternatives (system vs. brewed python), the Homebrew team cannot support you with issues.

Python 2.x or Python 3.x

Homebrew provides a formula for Python 2.7.x and one for Python 3.x. They don't conflict, so you can install them both. The executable python will always point to the 2.x and python3 to the 3.x version. But which version should I use?

Homebrew provided Python bindings

Some formulae provide python bindings. Sometimes a --with-python (or similar) option has to be set. Check with brew options <formula>. As the time of writing (Dec. 2012), almost all bindings are installed for Python 2.x only. There is an issue to discuss this topic.

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