Installing iPython On OS X Mountain Lion

ipython plot example

An iPython notebook showing a plot.


 

NOTE: There is now a much easier and powerful way to install IPython on the Mac. See my post, ‘IPython Install Made Easy for Macintosh’ for details.


 

I have to admit I really like Python. As languages go it has me impressed. If you take python and combine it with the power of a good shell you get iPython. I’ve found iPython extremely useful for system administration. Installing basic iPython is fairly easy, installing it so you get all its power is a little harder.

This install process assumes you have nothing yet installed on a clean system, you may have already installed some things.

To test this properly I started by creating a fresh install of Mountain Lion on an external drive.

For some of the installs you will need the Xcode command line tool chain. Install XCode then open “Preferences” and click on the “Downloads” tab. This allows you to install the XCode command line tools.

Now there is an argument about the various package managers for OS X. The three available are Fink, MacPorts and Homebrew. Personally I used to use Fink then MacPorts and I’ve now decided that Homebrew is the best of the bunch. Certainly Homebrew seem better at keeping their packages up to date and working.

So we need to install Homebrew.

ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)

It will ask you to press return once or twice and ask for your password so it can run ‘sudo’ but otherwise the command above just works. After it installs it advises we should run ‘brew doctor’ before installing anything so go right ahead and do that.

Now to make sure the system can find everything Homebrew installs we need to add to our path.

vi ~/.profile

will open up our profile and then we add

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

and save the file.

Now we need to install ‘pip’.

sudo easy_install pip

Now for virtualenv. virtual env is a tool to create isolated Python environments, it allows us to install all sorts of goodies without worrying that we will upset other virtualenv envronments. We will also install virtualenvwrapper, a set of extensions for making working with virtualenv a little easier.

sudo pip install virtualenv
sudo pip install virtualenvwrapper

Next source the virtualenvwrapper script

source /usr/local/bin/virtualenvwrapper.sh

This creates a hidden directory (“~/.virtualenvs”) to hold your virtual environments.

Now we create one

mkvirtualenv ip

You will notice that your prompt now starts “(ip)” – that tells you the virtual environment you are in.

Now we are ready for iPython.

pip install ipython

Now Apple, in their infinite wisdom, have chosen to use a readline library that doesn’t conform to the Unix standard so we need to install one that does. The least intrusive one to install for our purposes is the python one. It’s not the fastest but it does install easily and it’s all we need for our purposes.

pip install readline

We now have the basics done. If you are only going to use iPython for system administration then you are done.

Frankly I also like to at least install the packages for iPythons notebook system.

pip install pyzmq
pip install tornado

Maths and Science

For maths and sciences you might want to add a couple of packages.

Install numpy

pip install numpy

We need to install Fortran for scipy.

brew install gfortran

Install Scipy

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

Getting A QT console

One of the nicer additions to iPython is a standalone QT console that works well and can display graphs if you install matplotlib. It also has a few requirements.

First pygment for syntax highlighting.

pip install pygment

Then you need to bring out brew again for a couple of other system requirements. Note that all of these have a bunch of dependencies so installing them is not a quick operation.

brew install freetype
brew install libpng

Then matplotlib. For Mountain Lion you will have to install the development version so grab it direct via git.

pip install git+https://github.com/matplotlib/matplotlib.git#egg=matplotlib-dev

Finally install pyqt.

brew install pyqt

Now you have iPython installed with all possible options.

Check the documentation on how to run it in the various modes.

Conclusion

Once you’ve done all that you will have a superb environment for working with Python for all manner of tasks. Below I’ve listed a bunch of sites you will find useful.

Recommended Reading

Advertisements

16 thoughts on “Installing iPython On OS X Mountain Lion

  1. When i try: ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)
    I get: Missing name for redirect
    😦

  2. 1. it should be pip install pygments (with an s)
    2. There’s a small formatting error above on the scipy install command (not in a pink box)
    3. a final test command might be nice, e.g. ipython notebook, or ipython qtconsole, or something
    Otherwise, got all the way through. Thanks!

  3. Couple more quick ones:
    1. it should be ~/.profile
    2. mkvirtualenv makes a .virtualenvs (with an s) directory

    • Good point. First, that method wasn’t available when I wrote the instructions.

      The main reason is that by installing all the bits yourself you know what you have installed. It also allows you to use virtualenv to fence any changes you make from the main python install. My list also installs some maths stuff you don’t get with the easy_install.

      Also, by knowing the piece by piece way you can adapt when one of the components has a problem. At one point one of them required patching for OS X, can’t remember which.

      Finally, it allowed me to evangelise brew and virtualenvwrapper.

      • All very reasonable! I just wanted to make sure I wasn’t completely confused about what I am about to get into 🙂 Thanks again!

  4. Thank you for this helpful article. But I faced a problem at the last stage, namely qtconsole.
    I followed your guide, and installed pyqt( it took some time on my mac to compile). Then I use ipython qtconsole hoping to invoke gui ipython, but it failed. The prompting is:

    Could not load requested Qt binding. Please ensure that
    PyQt4 >= 4.7 or PySide >= 1.0.3 is available,
    and only one is imported per session.

    Currently-imported Qt library: None
    PyQt4 installed: False
    PySide >= 1.0.3 installed: False
    Tried to load: [‘pyside’, ‘pyqt’]

    How to check if I installed pyqt correctly on my virtualenv, and how to solve this issue?

      • I have the same problem.I solved this by copying PyQt4 and sip to ip’s site-packages:
        cp -r /usr/local/lib/python/site-packages/PyQt4 ~/.virtualenvs/ip/lib/python2.7/site-packages/PyQt4
        cp /usr/local/lib/python/site-packages/sip.so ~/.virtualenvs/ip/lib/python2.7/site-packages/

  5. I’ve gone through this process of installing django, oscar, postgres, postgis, ipython, and qtconsole (with matplotlib, etc) on both Mac OSX and WIndows Vista a few times in the past year and a half. Thanks to all of you awesome folks with tidbits of info that seem to appear each step of the way to help me out!

    As an alternative to copying the site-packages folders over to the new virtualenv site-packages folder (though that works, too), I used this command to get around the problem with pip install pyside or pyqt4 on my mac.

    ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future sudo pip install pyside

    It took about an hour, but it worked. Also, now have to brew install gcc instead of gfortran.

  6. I’ve followed the steps but when I was at “brew install pyqt” it prompted “Phthon support is broken”. And it seems that without it the “ipython qtconsole” doesn’t work and the Qt just can’t be found..

  7. At first I got the “Could not load requested Qt binding. ” error that everyone else above got. Then I tried:

    cp -r /usr/local/lib/python2.7/site-packages/PyQt4 /usr/local/lib/python3.4/site-packages

    cp -r /usr/local/Cellar/sip/4.16.1/lib/python2.7/site-packages/sip.so /usr/local/lib/python3.4/site-packages

    Now I get:

    ImportError: dlopen(/usr/local/lib/python3.4/site-packages/sip.so, 2): Symbol not found: _PyCObject_Type
    Referenced from: /usr/local/lib/python3.4/site-packages/sip.so
    Expected in: flat namespace
    in /usr/local/lib/python3.4/site-packages/sip.so

    I assume this is some kind of version mismatch, since I lifted PyQt4 out of the python2.7 folder and I am now using it with Python 3.4.

    I am curious if everyone goes through this when working with Python? I’ve worked with Ruby and Java but I am only now starting to use Python. I spent 6 hours yesterday trying to get setup and I have spent about 8 hours today trying to get setup, debugging all the errors. I keep thinking that there must be a way to install Python and not spend 14 hours on it? Or is this typical?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s