December 3, 2011

Installing ReportLab's Open Source Package on a Mac

I'm setting up to do some development, and this has meant installation work–never my favorite task, but  man's got to do what a man's go to do.

I mostly (i.e. when it there's a recipe for it) use homebrew to install functionality on my Mac. This isn't a religion, just my own particular choice, and by and large I'm comfortable with it. The current homebrew recipe for Python offers an easy update to 2.7 (while retaining 2.6 as the “system” Python) and includes the distribute package. This means you can immediately run

    easy_install virtualenvwrapper

and fixed up your shell initialization per the installation instructions you are ready to go. Once I'd done that I created a virtual environment with

    workon test1

and installed the recent PIL fork–which I felt would be a good compatibility test–with

    pip install pillow

The virtual environments you create with virtualenvwrapper already have pip installed, which is neat. Next I thought I'd see whether I could just do a vanilla install of the reportlab package, which is admirably good at writing PDF files. With some delight I observed

    pip install reportlab

appeared to go out to the Internet for the package, download and correctly install it. Except there was this annoying message about how it hadn't been built with freetype2 support.


    # installing without freetype no ttf, sorry!
    # You need to install a static library version of the freetype2 software


Hmm, no problem, uninstall it again (thank you pip) and install freetype2. Except that

    brew install freetype2

shows me that there's no brew formula for the dratted package, and there's (what seems to be) a perfectly good freetype2 library in /usr/X11. This is kind of annoying (there must be hundreds of Mac users of ReportLab's stuff, surely?) but I downloaded the source and tweaked around with it to see if I can get it to “see” the X11 library (all the time wondering whether that's some X-dependent stuff I don't really want to tangle with). Eventually I realized it didn't matter. Without writing a brew recipe for reportlab (which is what really seems to be required—that would allow patching of setup.py—but how could we persuade Python users not to user easy_install?) I needed a freetype2 that the standard pip install could see.

So I just had to go out in the hopeful spirit of yak-shavers everywhere and download and install freetype2 from source. Exactly what I was hoping to avoid, but I don't remember it being that difficult. I also remember blessing the author for including an “uninstall” target to try and remove it, which seems to work pretty effectively. Maybe I'll see if I can write a brew formula and submit it. Anyway, I now had a second copy of freetype2 happily occupying its own little corner of /usr/local, and that was all that was required since the standard reportlab build appears to look there for libraries.

Which meant that pip installed the package beautifully, complete with freetype2 (and therefore TrueType font) support. Hooray!

10 comments:

tomc said...

Just went through setting all this up on Lion. Here is what worked for me:

Freetype2 from source requires GNU Make 3.81 or higher.

In the source directory:
$ ./configure
$ make
$ sudo make install

For reportlab:
$ easy_install pip
$ pip install reportlab

Chris Topinka said...

Found symlinks do the trick. Here, https://groups.google.com/forum/?fromgroups=#!topic/python-virtualenv/1C-P0sUuiLc

complicatedlives said...

I have installed freetype2 successfully from source, but Reportlab still doesn't recognize it when it installs. I tried cmtopinka's suggestion, and it broke something even worse - prior to creating those symbolic links, I could at least install and uninstall reportlab cleanly using pip; now it won't even install, and generates pages of error messages in the process.

I'm a total newbie here and cannot for the life of me figure out how to remove those symbolic links now that they've been created...I know there are probably better places to ask this, but I'm casting a wide net.

Everything I can find in google suggests to use rm to remove a symbolic link, but for example, having created a link with the following:

ln -s /usr/X11/include/freetype2 /usr/local/include/

the best I can figure the way to remove that link would be

rm /usr/local/include/ /usr/X11/include/freetype2

But that doesn't work.

The most relevant thread I could find on the official UNIX board consisted of someone chewing out a noob for asking such an obvious question, and refusing to provide the answer, but, like whoever posted it, I can't figure it out...

Steve said...

Sorry about the chewing out you saw on the list. People aren't always as friendly to struggling noobs as they ought to be. Take no notice. We all struggle sometimes.

Since you don't give any error messages I can only explain the symbolic links thing. When you enter

ln -s /usr/X11/include/freetype2 /usr/local/include/

at the command line that creates a symbolic link called freetype2 in tne /usr/local/include directory. You should be able to remove that link simply with

rm /usr/local/include/freetype2

complicatedlives said...

That worked! Thanks so much!

However, my Reportlab still does not recognize that I've got Freetype installed, even though I do. I've uninstalled and reinstalled both several times, to no avail. I'm running Python 2.7 on OSX 10.6, fwiw.

Since you've been so helpful, beyond that, the problem I'm having is that, when I run the scripts I've written using Reportlab, they complete without an error message, but don't actually create a PDF as output. (each one ends with a call to renderPDF.DrawToFile(), but when I search for the filename I provide in the arguments, the search comes up blank).

Someone suggested I needed Freetype installed in order for Reportlab to have access to fonts with which to create a file, and that without it it won't produce output because it can't create anything, so that's what's set me down this path...I can't find anything about the problem I'm having via Google searching, so if anyone here has any insight on this it would be greatly appreciated...

I've installed Freetype via both the methods outlined in http://www.cmlenz.net/archives/2011/07/building-pil-with-freetype2-on-snow-leopard , but neither - installing directly from the tarball, or using pip to install Pillow - is recognized by Reportlab; I still get 'installing without freetype no ttf, sorry! # You need to install a static library version of the freetype2 software' when I build Reportlab.

I thought the answer provided in the comments about creating symbolic links so Reportlab could find the Freetype library was the answer I was looking for, but obviously it didn't work for me...

Any encounter this problem before and know where the answer might lie?

Steve said...

The "no PDF" problem could be as simple as you forgetting to close down the stream at the end of your program. I presume the file is just never created?

ReportLab comes with a very good test suite, and I would encourage you to find out how to run it.

I would also advise you (if you haven't already, whcih I'm guessing you haven't) to install virtualenvwrapper and learning how to use it. It makes working on multiple projects much more practical.

Also, when they have time the Reportlab staff are quite responsive to inquiries via open source channels such as comp.lang.python. I remember having difficulty getting the installation to recognize my FreeType, but that was a long time ago. Good luck!

complicatedlives said...

Wow, thanks again for the prompt response.

Yes, the PDF is never created. I installed virtualenv and tried to install virtualenvwrapper but got an error and haven't gone back and tracked it down (can only handle so many errors at a time ;)

I have run the Reportlab test, and it generated a bunch of pdf's as output...which until now I'd never considered as refuting my theory that it lacks a necessary component to do so.

If you'll indulge me further, what would 'closing down the stream' look like? I'm working out of Magnus Lie Hetland's book Python: From Novice to Professional, and, to copy & paste the simplest program that involves Reportlab:

from reportlab.graphics.shapes import Drawing, String
from reportlab.graphics import renderPDF

d = Drawing(100, 100)
s = String(50, 50, 'Hello, world!', textAnchor='middle')

d.add(s)

renderPDF.drawToFile(d, 'hello.pdf' )

Is there something further I need to do to 'close down the stream'?

Again, I greatly appreciate all the help...

Steve said...

Apparently not. I just ran the same code on my computer and it dutifully created a hello.pdf in the current directory. Are you sure it isn't there somewhere?

complicatedlives said...

Well, I'm an idiot. It does create the file, it just doesn't show up in a finder search for some reason.

Thanks very much for your gracious help...I'll go back to trying to learn enough about this to not to have to ask such basic questions now. ;)

The upside to all of this is that it led me to install homebrew, which has made installing packages far easier than it had been before...I just tried virtualenvwrapper again and it worked fine.

Onward...

Steve said...

If that was the most idiotic thing I'd done in my programming career I'd be happy. I'm glad you've come to brew - I used it from the start on my Mac, and it's been very useful.

Good luck with your Python!