· python

Python: matplotlib - Import error ft2font Symbol not found: _FT_Attach_File (Mac OS X 10.8.3/Mountain Lion)

As I mentioned at the end of my last post about the Titanic Kaggle problem our next step was to do some proper machine learning™ using scikit-learn so I started by looking at the Decision Tree example.

Unfortunately I ended up on the mother of all yak shaving missions while trying to execute the code which draws a chart using matplotlib.

I ran the following line from the tutorial:


import pylab as pl

which lead to this exception:


ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib/ft2font.so, 2): Symbol not found: _FT_Attach_File

I found this post on Stack Overflow which had a variety of suggestions one of which was to attempt to install matplotlib from a dmg on the website.

I tried that and got this error when trying to do so:


matplotlib 1.2.1 can't be installed on this disk. matplotlib requires System Python 2.7 to install.

I do have Python 2.7 installed so the error surprised me. In any case, I eventually came across a post on Tao of Mac which explained how to install matplotlib from source:


sudo brew install pkgconfig
cd /tmp
git clone git://github.com/matplotlib/matplotlib.git
cd matplotlib/
python setup.py build
sudo python setup.py install

That installed successfully so I went back to the example which now failed on an earlier import statement:


>>> from sklearn.tree import DecisionTreeRegressor
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ImportError: No module named sklearn.tree

I tried to reinstall scikit-learn:


$ sudo easy_install scikit-learn

That installed successfully so I tried to import the DecisionTreeRegressor again:


>>> from sklearn.tree import DecisionTreeRegressor
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/scikit_learn-0.14.1-py2.7-macosx-10.8-intel.egg/sklearn/__init__.py", line 32, in <module>
    from .base import clone
  File "/Library/Python/2.7/site-packages/scikit_learn-0.14.1-py2.7-macosx-10.8-intel.egg/sklearn/base.py", line 10, in <module>
    from scipy import sparse
ImportError: No module named scipy

I tried again and got this error message instead:


>>> from sklearn.tree import DecisionTreeRegressor
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/scikit_learn-0.14.1-py2.7-macosx-10.8-intel.egg/sklearn/__init__.py", line 31, in <module>
    from . import __check_build
ImportError: cannot import name __check_build

I tried to reinstall scipy to see if that would help:


$ sudo easy_install scipy

but that led to the following error:


no previously-included directories found matching 'scipy/special/tests/data/boost'
no previously-included directories found matching 'scipy/special/tests/data/gsl'
no previously-included directories found matching 'doc/build'
no previously-included directories found matching 'doc/source/generated'
no previously-included directories found matching '*/__pycache__'
warning: no previously-included files matching '*~' found anywhere in distribution
warning: no previously-included files matching '*.bak' found anywhere in distribution
warning: no previously-included files matching '*.swp' found anywhere in distribution
warning: no previously-included files matching '*.pyo' found anywhere in distribution
Could not locate executable f95
Could not locate executable f90
Could not locate executable f77
Could not locate executable xlf90
Could not locate executable xlf
Could not locate executable ifort
Could not locate executable ifc
Could not locate executable g77
Could not locate executable gfortran
Could not locate executable g95
Could not locate executable pgf90
Could not locate executable pgf77
don't know how to compile Fortran code on platform 'posix'
error: Setup script exited with error: library dfftpack has Fortran sources but no Fortran compiler found

I came across this StackOverflow post which suggested installing the Fortran compiler using brew:


$ brew install gfortran

but that didn't work too well either:


==> Installing gfortran dependency: isl
==> Downloading http://ftp.de.debian.org/debian/pool/main/i/isl/isl_0.11.2.orig.tar.bz2

curl: (22) The requested URL returned error: 404
Trying a mirror...
==> Downloading ftp://ftp.linux.student.kuleuven.be/pub/people/skimo/isl/isl-0.11.2.tar.bz2

curl: (28) connect() timed out!
Trying a mirror...
==> Downloading http://www.kotnet.org/~skimo/isl/isl-0.11.2.tar.bz2

curl: (22) The requested URL returned error: 404

Instead I downloaded a tar.gz package from HPC and installed it manually:


$ gunzip gfortran-4.8-bin.tar.gz 
$ sudo tar -xvf gfortran-4.8-bin.tar -C /

After that had finished I tried reinstalling scipy using pip this time (based on a suggestion in this post):


$ sudo pip install scipy

And now I can successfully run the tutorial!

----------------

At some stage in the process I also upgraded numpy but I'm not sure if that's necessary to get things working. Just in case:

~~~bash

$ sudo pip install numpy –upgrade

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket