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

1
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

1
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):

1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then I did a fresh Python 3 installation:

1
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:

1
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:

1
echo $PATH

which might look like this:

/Library/Frameworks/Python.framework/Versions/3.4/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/subversion/bin:/sw/bin:/sw/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:

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:

1
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:

1
which python3

which should echo

/usr/local/bin/python3

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:

1
2
3
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 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *