Installing Python 3 and music21 using Homebrew and pip3

In this blog post I’m going to explain how the Python library music21 can be installed in conjuction with Python 3 and its dependencies matplotlib, numpy and scipy on Mac OS X 10.10 (Yosemite). It can also be used as a tutorial for installing any other Python libraries/modules as well.

The Problem

Initially, on my system there were two parallel Python 2 and Pyton 3 installations. The music21 installer chose Python 2 as default installation target. In order to use music21 in conjuction with Python 3, I tried to install it using the command

pip3 install music21

which worked fine. However, when I tried to use the plotting capabilities of music21 an error occured due to the missing modules matplotlib, numpy and scipy. When trying to install matplotlib issuing

pip3 install matplotlib

the following error occurred:

SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.

Installing Python 3 using Homebrew

My final solution to this problem was to set up a new Python 3 installation using  Homebrew. This is done by installing Homebrew (if you haven’t got it yet):

ruby -e "$(curl -fsSL"

Then I did a fresh Python 3 installation:

brew install python3

If you already have Python 3 installed, Homebrew will not be able to create symlinks to the python binaries since they already exist. To overwrite the existing symlinks (and thus to set the Homebrew Python as default interpreter for your system) you have to execute this command:

brew link --overwrite python3

Adjusting the $PATH Variable

Now the correct symlinks are created under /usr/local/bin, but unfortenately the old Python 3 installation is still preferred by the system because there is a PATH entry with higher priority. You can view your PATH order with this command:

echo $PATH

which might look like this:


As you may notice the old interpreter entry /Library/Frameworks/Python.framework/Versions/3.4/bin preceeds /usr/local/bin. To remove this entry, you copy the PATH contents without the old Python 3 path (in this example from the first colon to the end) and set a new PATH:

export PATH=/opt/local/bin:[more pasted path elements here]

After that our Python 3 installed with Homebrew should now be the default system interpreter. You can test this with the following command:

which python3

which should echo


which is in turn a symlink to the Homebrew cellar (the place where Homebrew installs applications/modules).

Installing the Dependencies

Now you should be able to install music21 and the dependencies using pip3:

pip3 install music21
pip3 install matplotlib # this will install numpy automatically
pip3 install scipy

I hope this will help you to install a clean music21 environment. No go have fun with musical analysis and plotting 🙂