OBSOLETE: MacPorts Python in Astrophysics

OBSOLETE: This document describes how to use MacPorts's Python libraries, and to install other packages into your own library directory, without using a virtual-Python setup.

PLEASE NOTE: What follows is an extremely quick-and-dirty guide, which has become out-of-date to the point of danger, and which has been superceded for most purposes by our instructions for using virtual Python environments. This page is left for those who may not wish to use virtual Python: it is not known at the time of writing whether Python development environments (eg PyCharm) will play nicely with virtual Python setups, as these try to address the same problem in different ways.

Consider yourself Warned.

Please see:

One-off preparation steps:

Download the file shown at the top-right of this page, place it in your home directory, and rename it to .pydistutils.cfg.std; then:

  • ln -s .pydistutils.cfg.std ~/.pydistutils.cfg
  • mkdir -p ~/Library/Python/2.7/site-packages
  • mkdir -p ~/bin

Each-time preparation steps:

In a fresh shell, do, in this order:

  • Read the Notes below. Not all steps are necessary.
  • Activate scisoft # See Note 1 below
  • Activate python-macports # See Note 2 below
  • Activate python-mylib # Required: See Note 3 below


  1. Use this line only if you need Python packages which are in SciSoft or the AstroPack which are not already in MacPorts. Try first without it. (We intend to eventually phase out SciSoft and the Python AstroPack in favour of MacPorts.)
  2. This line causes MacPorts's Python modules to dominate SciSoft's. Use this only if you have had to Activate SciSoft.
  3. This causes your personal libraries to dominate the others.

Advice: work out the minimum necessary and sufficient set of actions. If in doubt, try the last line only, and see how far you get; and remember to start a fresh shell for each test.


To install the Python suite foosuite, do:

  • pip-2.7 -v install foosuite

Thereafter, foosuite should be available to Python for you.

Problems with matplotlib:

Attempts to run matplotlib demos (eg poly3d_demo.py) using the matplotlib in MacPorts's Python library set can yield an error avalanche, ending in:

AttributeError: 'ZAxis' object has no attribute '_set_scale'

.... and indications that code from /System/Library/Frameworks/Python.framework/ is being used. This is apparently due to a bug in matplotlib: its toolkit code seems to not load itself properly, whatever your $PYTHONPATH may wish to say about the matter.

The kludgearound for this is to ensure you're using MacPorts's Python executable (/opt/local/bin/python), rather than the system's (/usr/bin/python). This is agreed to be awkward at best.

Please note:
Resist the temptation to put /opt/local/bin at the beginning of your $PATH, especially in your shell startup files, unless it is already there for other reasons (eg for readers in Theory). This is one of those Simple, Neat, Obvious and Wrong things advised on the Net by those who aren't working in a production environment like ours: it does ensure that MacPorts's Python is used in preference to OS X's, but The Bad News includes (but is far from limited to) the fact that MacPorts's svn will also dominate OS X's. This can break local Subversion checkouts in interesting and mystifying ways. (It's possible to upgrade the svn checkout to match, but there's no way back. Terms like "slippery slope" and "blown out of the water" come to mind.)

Alternatives which are more user-friendly and less invasively toxic are under development.

Categories: Astro software | Astrophysics | Mac | OS X | macports | python