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.
will open up our profile and then we add
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
This creates a hidden directory (“~/.virtualenvs”) to hold your virtual environments.
Now we create one
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.
pip install numpy
We need to install Fortran for scipy.
brew install gfortran
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.
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.
- iPython You can find the documentation here.
- Ferndando Perez Great blog for scientific computing with iPython
- Demonstration of the ‘IPython’ Interactive Shell Video tutorial