Installing Python libraries on OS X without administrator privileges

You may want to install and maintain Python libraries on your Mac in the following circumstances:

It is easy to install these libraries in your home directory. You do not need administrator privileges to do this. The following steps have been tested and are confirmed to work for the following packages: numpy 1.1.1, numpy 1.2.0, ipython 0.9.1. scipy is the exception to the rule and is covered separately.

Please note: This is a systems administration task. If you choose to install libraries yourself then you should not expect help from your system administrator. This is, however, a documented feature of Python designed for the specific situations described above. It’s very difficult to do any harm by following the instructions below, and you can always delete or move the relevant directories if you end up with a non-functioning installation of numpy, for example. The details of what is going on are covered in Peak’s Guide to EasyInstall. See in particular the section about Mac OS X User Install. For an explanation of why Step 5 of the instructions below is sometimes necessary, see the section on Administrator Installation (which steps have not been performed on the Oxford Astrophysics system).

Installing everything except scipy

  1. Create a file ~/.pydistutils.cfg which contains the following lines:

    [install]  
    install_lib = ~/Library/Python/$py_version_short/site-packages  
    install_scripts = ~/bin
    
  2. Make sure ~/bin is in your shell’s path. (Of course you could use another directory here, provided you have write access to it.) For example, I use bash, so have the following lines in my ~/.bashrc:

    if [ -d ~/bin ] ; then  
        PATH=~/bin:"${PATH}"  
    fi
    
  3. Run the following command to create place for the Python modules you install to live:

    mkdir -p ~/Library/Python/2.5/site-packages/
    
  4. Some packages have to be built in slightly different ways (e.g. scipy), but the majority can be built and installed in either of the following ways, which are essentially equivalent:

    1. Run the command easy_install numpy. Replace numpy with any package. That’s it. I easy_install numpy, ipython, readline and nose on every system I use Python on.

    2. Download the source of the Python library that you want to install. Unzip the file, change into the directory created, and run python setup.py build. This builds the library and executables. If this finishes without error then run python setup.py install, which copies the library files and executables into the directories specified in ~/.pydistutils.cfg.

  5. Run python at the command line and try, e.g. import numpy. If you downloaded the source manually then make sure that you change out of the source directory before doing this. If import module works then run a few lines using the module to test it (numpy.test(1, 10) works well for numpy). If you’re concerned that you may have just imported the sitewide numpy library then you can print the contents of numpy.__version__, which should match the version you just installed. If you do not get the version number you expect or the module is not found then proceed to step 6. Otherwise, you’re all done.

If you’ve installed Python package which is meant to be run from the command line (such as ipython or mercurial) then run those commands from your command line to check that the scripts installed correctly.

  1. Not necessary on all systems: on some systems (including the Oxford Astro system as it is presently configured), Python does not search the default directory for user-installed Python libraries, which we specified in pydistutils.cfg. To fix this, you have to manually add this directory to your PYTHONPATH shell variable. In bash I do this by adding the following lines to my ~/.bashrc:

    if [ -d $HOME/Library/Python/2.5/site-packages/ ]; then  
      export PYTHONPATH=$HOME/Library/Python/2.5/site-packages/  
    fi  
    

Once the PYTHONPATH variable is set (you may have to quit and relaunch your Terminal) you should perform the test described in step 4 to check that Python can find your library.

Installing scipy

Scipy is a set of Python wrappers around various public Fortran numerical codes, including netlib. Despite the name it is not, in general, necessary to install it in order to do scientific computing using Python. If you don’t know you need it, then don’t bother installing it. If you do need it then it can be installed as follows. (These instructions were tested with scipy–0.6.0 and numpy 1.2.0.)

  1. scipy requires numpy so make sure you’ve upgraded numpy to the current version using the steps above.

  2. Install a more reliable Fortran compiler using the instructions here.

  3. Download and unzip the source distribution of scipy. Change into the directory created and run the following single command:

    python setup.py build_src build_clib --fcompiler=gnu95 build_ext \ 
    --fcompiler=gnu95 build
    

The --fcompiler switches tell Python to build scipy using gfortran rather than g77 (which is even less use than the buggy gfortran).

  1. Run python setup.py install and test your installation by changing out of the scipy source directory (important!), running Python, importing scipy, running scipy.test(1, 10). scipy 0.6.0 has a known failure in scipy.test on OS X so as long as most of them are passed you’ve probably got a working installing.