This post is about Apple + python, not apple pie though that would be a yummilicious post too.
Before I begin, I really am no coder. I say this, because I’m not, really, am not. Okay, I started learning python and coding for my job but I’m not a coder, seriously, I swear!
Now, one of the great things of Python is that OS X ships with it… well kinda. Every version of Mac OS X ships with a version of Python which by the time you get to it is hopelessly outdated. Developing against Apple’s Python is not always the nicest thing but a necessity when you’re using PyObjC for example to build a Mac app.
However, when you’re just coding, it would be nice to have a completely up to date Python installation which does not touch or contaminate the systemwide Python in any case. There are a few ways to do it, MacPython, MacPorts, Fink and brew. The only good way. is brew!
MacPython is nice but it contains some bugs now that XCode 4 is out, namely that it distutils then still looks for powerpc compilers which you don’t have anymore blowing up every single compile that involves C-modules. Also, I don’t like the “framework” distribution model much but that’s just a personal preference.
MacPorts and Fink I loathe. I do not like their policies, I do not like the fact that they override my path and replace every god damn binary in stead of using whatever Apple supplies to maximise compatibility and add to that. I do not like that they put stuff in /opt or /sw, it’s ugly and since Mac OS X is posix compliant and Unix certified, we can use /usr/local just fine, as Apple intended.
Brew however got it completely right (except that it is written in Ruby
). Brew utilises /usr/local, which is left empty and available to the user to do what you want. It does not replace binaries and libraries provided by Apple (except in very rare and well thought out situations) and, once set up does not require you, at all, to sudo every again to install your software.
So, just go with brew, okay?
Though Apple supplies Python, brew decided that they can do it better, and in such a way that it doesn’t conflict with Apple’s Python. How they do this is explained over here: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python
I’ll run you through it though:
brew update
brew install python
Now edit your ~/.profile or whatever and modify your path like so:
export PATH=/usr/local/share/python:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11/bin
Now do a source ~/.profile and a which easy_install should now give you a binary at /usr/local/share/python/easy_install. As you can see you’re now picking up on the Python interpreter installed by brew, site-packages and other scripts and not Apple’s anymore.
Next up:
easy_install pip
pip install --upgrade distribute
You’re done!
You know have Python (2.7.x) with a version of distutils that will not try and find powerpc compilers should you have XCode 4 installed and you can mess around and code against it without blowing up the system wide Python and thus possibly OS X.
One thing: because you modify your path through ~/.profile and not ~/.MacOSX/environment.plist (see previous post) GUI applications will not pick up on the brew version of Python. This is usually what you want since Mac OS X GUI applications written in Python/PyObjC should just work against Apple’s Python. Should you really want to launch a GUI app with a newer version of Python (at your own risk) you need to launch it through the terminal explicitly starting it with the python2.7 interpreter.





0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.