Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import error ft2font from matplotlib (python, macosx)

I was installing matplotlib to use basemap today when I had to install a lot of stuff to make it work. After installing matplotlib and be able to import it I installed basemap but I can't import basemap because of this error:

from mpl_toolkits.basemap import Basemap

Traceback (most recent call last): File "", line 1, in File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/mpl_toolkits/basemap/init.py", line 36, in from matplotlib.collections import LineCollection File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib/collections.py", line 22, in import matplotlib.backend_bases as backend_bases File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 38, in import matplotlib.widgets as widgets File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib/widgets.py", line 16, in from lines import Line2D File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib/lines.py", line 23, in from matplotlib.font_manager import FontProperties File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib/font_manager.py", line 52, in from matplotlib import ft2font ImportError: dlopen(/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib/ft2font.so, 2): Symbol not found: _FT_Attach_File Referenced from: /usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib/ft2font.so Expected in: dynamic lookup

So when I tried to import ft2font in python by:

from matplotlib import ft2font

I got this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib/ft2font.so, 2): Symbol not found: _FT_Attach_File
  Referenced from: /usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/matplotlib/ft2font.so
  Expected in: dynamic lookup

Any idea what to do? I'm using Mac OSX 10.6 and python 2.7.2 installed by homebrew.

like image 820
Tomas K Avatar asked Sep 21 '11 16:09

Tomas K


3 Answers

I ran into the same issue. Even after running make.osx, it still complained about _FT_Attach_File being undefined when I imported ft2font from matplotlib. Here's how I tracked down the problem. Hopefully, it will help someone else.

Running otool -L ft2font.so yielded:

    ft2font.so:
/Users/jbenuck/mpl_build/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)

Note the absence of any mention of libfreetype! How is ft2font.so supposed to locate the symbol if it isn't linked against it?

My next step was to capture the commands used during the build:

    make -f make.osx PREFIX=/usr/local clean fetch deps mpl_build > output.txt

Searching this yielded the command that was used to compile the offending python module. I changed the value of the output file to be one in my local directory and ran it:

/Developer/usr/bin/llvm-g++-4.2 -bundle -undefined dynamic_lookup -isysroot / -L/opt/local/lib -arch i386 -arch x86_64 -L/usr/local/lib -syslibroot,/Developer/SDKs/MacOSX10.7.sdk -arch i386 -arch x86_64 -I/usr/local/include -I/usr/local/include/freetype2 -isysroot /Developer/SDKs/MacOSX10.7.sdk build/temp.macosx-10.7-x86_64-2.7/src/ft2font.o build/temp.macosx-10.7-x86_64-2.7/src/mplutils.o build/temp.macosx-10.7-x86_64-2.7/CXX/cxx_extensions.o build/temp.macosx-10.7-x86_64-2.7/CXX/cxxsupport.o build/temp.macosx-10.7-x86_64-2.7/CXX/IndirectPythonInterface.o build/temp.macosx-10.7-x86_64-2.7/CXX/cxxextensions.o -L/usr/local/lib -L/usr/local/lib -L/usr/lib -L/usr/X11/lib -lfreetype -lz -lstdc++ -lm -o ft2font.so

ld: warning: ignoring file /opt/local/lib/libfreetype.dylib, file was built for unsupported file format which is not the architecture being linked (x86_64)

Bingo! Problem found. I know I have both macports and homebrew installed. Apparently, one of them has a version of libfreetype in /opt/local/lib that isn't compiled for 64-bit.

I reran the command with "-L /opt/local/lib" removed which worked without a warning. Copying the resulting ft2font.so into my existing matplotlib installation now allows me to successfully import ft2font from matplotlib.

like image 130
Joshua Benuck Avatar answered Nov 08 '22 17:11

Joshua Benuck


In my case, this was an architecture problem - I had a 64bit version of freetype installed (that matplotlib happily compiled against) but when I ran in a 32bit version of python I got that error. The easy solution is to uninstall everything (freetype, matplotlib), and then install both the 32 and 64bit versions using homebrew and the --universal flag:

brew install freetype --universal

Note, I also had to do this for libpng as well (brew install libpng --universal). Not all homebrew recipes support the universal flag, but it's a huge help for the ones that do. (You can see a formula's options with brew info <FORMULA>).

Also, compiling using the make.osx Makefile in conjunction with homebrew was a total failure; in my experience I'd recommend one or the other.

like image 25
Erik Kastman Avatar answered Nov 08 '22 17:11

Erik Kastman


Okay, I figured it out.

I reinstalled matplotlib from source from github (https://github.com/matplotlib/) and then (instead of ordinary python setup.py install) I ran make.osx described in README.OSX:

make -f make.osx PREFIX=/devjunk PYVERSION=2.7 \
  clean fetch deps mpl_install_std

And everything works properly now.

like image 4
Tomas K Avatar answered Nov 08 '22 15:11

Tomas K