the further adventures of

Mike Pirnat

a leaf on the wind

« Previous Post Next Post »

Building Pycrypto on Snow Leopard and Non-Apple Python

I wanted to use PyCrypto on a personal project, which means that even though I will eventually deploy to Linux (where PyCrypto is super-easy to come by), I first needed to get it set up on my development machines, which are all currently Macs running Snow Leopard. This takes a bit of finagling--including some steps that I didn't see listed in (m)any other guides--so I figured it would be worth sharing (or at least memorializing here so I can find it again).

You may be tempted to just easy_install PyCrypto, or manually grab the source and try building from that. That's likely to get you a giant wall of compiler output that includes this lovely gem:

Compiling with an SDK that doesn't seem to exist: /Developer/SDKs/MacOSX10.4u.sdk
Please check your Xcode installation

But I already installed Xcode! you cry. What gives?!

Here's Trick Number One: install Mac OS X 10.4 support when installing Xcode. I hadn't, so I got to reinstall Xcode. Naturally, Apple knows better than you do, and won't let you install without the "Essentials" package, so go find something else to do while 8+ GB of stuff you already installed gets done all over again.

Okay, you think, now I'm set. This is going to be great! You pop back into the terminal and retry the build:

$ python setup.py build
running build
running build_py
running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -fno-common -dynamic -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/MD2.c -o build/temp.macosx-10.3-fat-2.6/src/MD2.o
In file included from /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/unicodeobject.h:4,
                 from /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:85,
                 from src/MD2.c:31:
/Developer/SDKs/MacOSX10.4u.sdk/usr/include/stdarg.h:4:25: error: stdarg.h: No such file or directory
In file included from /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/unicodeobject.h:4,
                 from /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:85,
                 from src/MD2.c:31:
/Developer/SDKs/MacOSX10.4u.sdk/usr/include/stdarg.h:4:25: error: stdarg.h: No such file or directory
lipo: can't figure out the architecture type of: /var/folders/FI/FIs1Mi6LFUeDQ-LNLRg+JE+++TI/-Tmp-//ccVhrG5I.out
error: command 'gcc' failed with exit status 1

Now you're confused. None of the existing "how to install PyCrypto" cheat sheets that you found had anything to say about this flavor of failure.

Upon reflection--and much Googling--you realize that this is your punishment for thinking you know better than Apple. You thought you were so clever when you installed a more current Python than what Apple ships, and you thought you were doing the right thing by going to Python.org to get the latest, greatest stuff. (Well, for some value of "latest, greatest", because like me you probably did this ages ago and never changed it once it was working.)

The problem is that Snow Leopard wants to use gcc-4.2 by default, but your Python.org-built Python wants gcc-4.0 instead.

Trick Number Two is that you need to tell it to do the right damn thing:

export CC=/usr/bin/gcc-4.0 
export CXX=/usr/bin/g++-4.0 

You can then python setup.py build and python setup.py install to your heart's content.

This second trick may be moot if your Python.org Mac Python binary is new enough--it looks like it got addressed in late 2009. Laziness, however, is one of the Three Virtues, and I find myself possessed of an abundance of it, so I have pretty much forgotten about keeping my Python current once I had something 2.6ish that was "good enough for now."

Now you know. And knowing is half the battle.

blog comments powered by Disqus

« Previous Post Next Post »