Skip to content

Instantly share code, notes, and snippets.

@klmr
Created March 8, 2012 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klmr/2001569 to your computer and use it in GitHub Desktop.
Save klmr/2001569 to your computer and use it in GitHub Desktop.
How to install Python with matplotlib on OS X

How to install Python with matplotlib on OS X

The following instructions are based on a blog post with some tweaks to install the patched pip packages in the default path rather than leaving them in a local source folder.

The following set of instructions is provided with some annotations. However, the code in each step can (and should!) just be pasted into the terminal window (except for the first part, which belongs in .bashrc).

Successfully tested with

  • Python 2.7.2
  • NumPy 1.6.1
  • SciPy 0.11.0.dev-b924f0b
  • matplotlib 1.2.x

Note that this installation may fail for other versions if they change how modules are treated in SciPy or matplotlib; furthermore, the Python version is hard-coded below.

Oh and by the way, this can take quite a while (compiling gfortran from source etc.) so grab a cup of coffee.

Edit $PATH manually in .bashrc:

Just add the following lines at a convenient location to the file ~/.bashrc:

PATH=/usr/local/bin:$PATH
PATH=/usr/local/share/python:$PATH
export PATH

Install Homebrew

Homebrew is a package manager for Mac that is superior in usage to MacPorts and Fink. However, it can occasionally run into trouble if it is installed side by side with either of those. It's therefore advised to have only Homebrew installed.

(The following command(s) may ask for super user rights.)

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

Install prerequisites

brew install readline sqlite gdbm pkg-config

Install & set up Python itself

This step installs Python 2.7 and makes it the system’s default Python version. This does not uninstall the pre-installed Python, it just deactivates it.

brew install python --framework --universal
cd /System/Library/Frameworks/Python.framework/Versions
sudo rm Current
sudo ln -s /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/Current

Install pip

pip is an easy package manager for Python that acts as a replacement for easy_install. Ironically, it’s installed using the latter.

/usr/local/share/python/easy_install pip

Install NumPy, SciPy & matplotlib

For the following operations, we first of all alias pip to its explicit path just in case one of the previous steps went wrong (this happened, and I’m not exactly sure where, or how to prevent it).

alias pip=/usr/local/share/python/pip

Next, use it to install all the rest.

pip install numpy
brew install gfortran
pip install -e git+https://github.com/scipy/scipy#egg=scipy-dev
pip install -e git+https://github.com/matplotlib/matplotlib#egg=matplotlib-dev

Let the cleanup begin …

The previous installations leave behind the sources, and were not properly staged into the Python path; instead, they are lying around in the user’s home folder. The following steps move them to their appropriate location and make sure Python finds them correctly. They also remove the now-redundant source files.

cd /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/Current/lib/python2.7/site-packages

SciPy

mv ~/src/scipy/scipy/ .

mv ~/src/scipy/scipy.egg-info/ .
rm scipy.egg-link
rm -rf ~/src/scipy
rm -f ~/src/pip-delete-*

matplotlib

mv ~/src/matplotlib/lib/matplotlib.egg-info/ .

for lib in $(cat matplotlib.egg-info/top_level.txt); do
    if [ -d ~/src/matplotlib/lib/$lib ]; then
        mv ~/src/matplotlib/lib/$lib .
    else
        mv ~/src/matplotlib/lib/$lib.py .
    fi
done

rm -rf ~/src/matplotlib
rm -f ~/src/pip-delete-*

Clean up easy-install paths

sed -i '' -e '/^\/Users\/.*\/src\/.*/d' easy-install.pth

To check that everything worked:

cd
which python
python

This should display /usr/local/bin/python as the binary’s path, and 2.7.2 as the version. Then type the following while still in the Python interactive console:

import numpy
import scipy
import matplotlib

print numpy.__version__
print numpy.__file__
print scipy.__version__
print scipy.__file__
print matplotlib.__version__
print matplotlib.__file__
quit()

In each case, the path should be somewhere in /usr/local/Cellar/python.

Finally, you can also remove the folder ~/src if you don’t need it for other purposes.

@mattions
Copy link

mattions commented Mar 8, 2012

Could be interesting to have a look @ http://pypi.python.org/pypi/virtualenv and also http://pypi.python.org/pypi/virtualenvwrapper to isolate the python environment :)

@klmr
Copy link
Author

klmr commented Mar 8, 2012

Yup, I explicitly ignored vitualenv in the above to keep it simple and since I’m guessing that people who need this mostly already know about it. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment