MacPorts Python installation on Mac - 10 easy steps

Please report any issues with these instructions here.

This page was originally designed for Astronomers, but most of the instructions are generic, and apply to anyone wanting to use Python via MacPorts.

Installing MacPorts

  1. Ensure that XCode 3 or 4 is installed (should be on the install DVDs for your Mac). If you like to live on the bleeding edge, you can purchase XCode 4 from the Mac App Store. To check if XCode is installed properly, you can type gcc in the command-line. If you get gcc: command not found, then XCode is not properly installed.

  2. Download the DMG image for MacPorts from here - be sure to pick the correct one for your MacOS X version. Mount the disk image and run the installer. Note that if needed, it is possible to install MacPorts without root/admin privileges - see here for more details.

Installing Packages

  1. Go to the terminal and update the package index:

    sudo port selfupdate

  2. To install most of the basic Python packages, run:

    sudo port install py27-matplotlib py27-numpy py27-scipy py27-ipython

    Note that this will probably take several hours, and is best done overnight.

  3. To install Astronomy packages, use for example:

    sudo port install py27-pyfits py27-pywcs py27-atpy py27-aplpy

    MacPorts will automatically take care of any dependencies! (by default SQL dependencies are not installed for py27-atpy, and the Montage dependency is not installed for py27-aplpy. If you want to install these, use sudo port install py27-atpy +sql) and sudo port install py27-aplpy +montage respectively).

    Note: if for any reason the installation gets interrupted (for example if you get disconnected) and you end up with an error, try running the installation command again. If this does not work, try cleaning the latest port that MacPorts was trying to install, e.g.:

    sudo port clean --all -f py27-aplpy

    then try running the installation again. If you are still having issues, please let me know!


  1. If it does not already exist, create a folder called .matplotlib in your home directory and copy the default matplotlibrc file to it:

    cp /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc ~/.matplotlib/

  2. Edit .matplotlib/matplotlibrc and change the following line

    backend : Agg


    backend : MacOSX

    This ensures that when you use Matplotlib interactively, a window will pop up. Optionally, you can uncomment and set the image.origin and image.interpolation lines to

    image.origin : lower
    image.interpolation : nearest

    These settings are likely to be the most useful for plotting astronomical data.

  3. Make this Python installation the default:

    sudo port select --set python python27
    sudo port select --set ipython ipython27

    Note that this is optional - you can also invoke this Python installation by using python2.7 and ipython-2.7.

Searching for packages

  1. To check if a package is available through MacPorts, you can do:

    $ port search pyfits
    py25-pyfits @2.4.0 (python, science)
    Python interface to FITS formatted files
    py26-pyfits @2.4.0 (python, science)
    Python interface to FITS formatted files
    py27-pyfits @2.4.0 (python, science)
    Python interface to FITS formatted files
    Found 3 ports.

    Be sure to install the one for the correct Python version (py27-* if you've been using the above instructions).

    Before installing the package, you can check what variants are available:

    $ port variants py27-aplpy
    py27-aplpy has the variants:
    [+]avm: Include support for AVM meta-data
    [+]ds9: Include support for DS9 region files
      montage: Include support for Montage reprojection
    [+]rgb: Include support for RGB images
      universal: Build for multiple architectures

    Variants listed with [+] are installed by default. To remove a default variant, use -variant. To include a variant not installed by default, use +variant. For example, the following will install APLpy with support for Montage, but not for RGB images:

    sudo port install py27-aplpy +montage -rgb

Installing packages not in macports

  1. When installing packages not in MacPorts, first make sure that you remove your .pydistutils file if you have one, then simply use:

    python install --user

    (do not omit --user and do not use sudo). This will place user installed packages in ~/Library/Python/2.7/lib/python/site-packages where they will automatically be picked up by the Python installation, without messing up the MacPorts file structure. Note that you can also install packages in a similar way using easy_install-2.7 and pip-2.7 by specifying the --user option.

    The reason for installing packages with --user is that if instead you install packages using sudo python install, the packages will be installed inside the MacPorts tree, but MacPorts won't be aware of it, so this could cause issues in future if the package is installed via MacPorts. As a rule of thumb, don't ever install anything into /opt/local/ other than via the port command. If you did mistakenly install packages to the MacPorts directory, just go to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages and remove the files relating to the package you installed.

Appendix: Available Python Astronomy Packages

      py27-aplpy @0.9.6 (python, science)
          The Astronomical Plotting Library in Python

      py27-asciitable @ (python, science)
          An extensible ASCII table reader and writer

      py27-atpy @ (python, science)
          ATpy: Astronomical Tables in Python

      py27-idlsave @0.9.7 (python, science)
          IDLSave - a python module to read IDL 'save' files

      py27-kapteyn @2.1 (python, science)
          The Kapteyn Package

      py27-montage @0.9.3 (python, science)
          Montage python wrapper

      py27-pyavm @0.1.4 (python, science)
          Pure-Python AVM library

      py27-pyfits @2.4.0 (python, science)
          Python interface to FITS formatted files

      py27-pyregion @1.0.1 (python, science)
          pyregion is a Python module to parse ds9 region files

      py27-pywcs @1.10-4.7 (python, science)
          pywcs is a set of routines for handling the FITS WCS standard

      py27-vo @0.6 (python, science)
          A Python library to parse, validate and generate VOTable XML files.

      py27-yt @2.1 (python, science)
          Astrophysical simulation Analysis and Viz

Copyright 2011, Thomas Robitaille