After starting out with Python on Ubuntu Linux, I’ve now for a good while been doing most of my sustained work on the Mac, currently Mac OS X 10.6. Unfortunately I’ve neglected to give proper attention to how Python is installed there and ended up with:
- Python 2.6.1 (Mac default version?) in
/usr/bin(also, 2.5.4, which I’m not sure how it got there) - Python 2.6.5 installed via MacPorts in
/opt/local/bin/. This is my default - I use
pipto install libraries, which end up in some ungodly place (something like/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/– SRSLY?) - Otherwise, mostly TextMate, and Git for version control. Django and Google App Engine, etc…
Now I’m preparing to set up 2.7 and 3.2, and I am unhappy with the haphazard state of things. So what are your favourite approaches to organising the code and libraries, and how have you wrestled the Mac into submission?
I’d like to continue using pip, but would like to have more control or at least understanding of what libraries for which version are getting installed and made accessible from where: I’ve had problems with installing py.test via pip and only being able to load it from the obsolete 2.6.1 Python, not my current 2.6.5 one. MacPorts has python_select, but it’s not overly helpful:
reason: chris$ python_select -l
Available versions:
current none python26 python26-apple
Most Python people I’ve asked don’t use MacPorts, which I don’t much like, but the stock Mac Python from python.org. I’ve also heard the recommendation to use virtualenv systematically, so what is the link to a good practical introduction?
virutalenvis a great tool and is very useful for managing multiple Python instances on most platforms. On Mac OS X, though, things are more complicated because the use of framework builds makes it common to encounter multiple instances of even the same major version of Python. I suggest you first understand and be comfortable with how to manage framework installs on Mac OS X before delving intovirtualenv.“
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/– SRSLY?”Yes, that’s the default location within a Python Mac OS X framework build. Most distributors of Python on Mac OS X use a framework build, each generally using a different root, because it makes it simpler to support multiple versions and to keep all the site packages installed for a particular Python instance together in one place.
For Mac OS X 10.6, Apple supplies a Python 2.6 (rooted at
/System/Library/Frameworks/Python.frameworkand symlinked to/usr/bin/python2.6) as well as a Python 2.5 (for compatibility with Mac OS X 10.5 – that’s the/usr/bin/python2.5you see). The python.org Mac OS X installers install to/Library/Frameworks/Python.framework. MacPorts uses/opt/local/Library/Frameworks/Python.framework.The frameworks include a
bindirectory (at../Versions/x.x/binin the framework); this is normally the default location where package scripts will be installed. It’s also where symlinks to the interpreter binaries are placed.Here’s a look at a python.org-style framework with multiple versions of Python installed:
Here’s a MacPorts
binincluding a number of additional site packages:If you install 2.7 or 3.1 Pythons using python.org installers or MacPorts, they are all rooted under
/Library/Frameworksor/opt/local/Library/Frameworksand will happily co-exist with other versions. To successfully use framework installs, you need to manage your shellPATH. In particular, you should ensure that the frameworkbindirectory of the Python(s) you want to use are on yourPATH. So for MacPorts, something like this in~/.bash_profile:or for the python.org installers:
In addition, generally you need to install a separate version of your favorite Python package management add-ons (like
pipor theeasy_installcommand fromDistributeorsetuptools) for each Python instance you use. Each one is installed into the default script directory (i.e. the frameworkbindirectory). MacPorts takes care of that automatically when you install its version of the package (sudo port install py26-piporsudo port install py31-distribute). Note that Apple supplies a set ofeasy_installlinks in/usr/binfor its Pythons and they install packages to/Library/Python. You need to make sure you use theeasy_installorpipappropriate for your instance of Python. Making sure the instance’s frameworkbindirectory is first in thePATHensures that.Note, if you use MacPorts you can use its
python_selectcommand to manage which Python instance is pointed to by/opt/local/bin/python. Then if/opt/local/binappears earlier in your PATH list than other directories with apythonit will govern which is your defaultpython. But you really need to manage the PATH by adding the frameworkbindirectory sopython_selectis generally not all that useful.Another positive aspect of using MacPorts (or similar broad-based distributor) is that it makes available many popular Python packages including handling the occasional messy details of building and linking to 3rd-party C libraries not shipped with Mac OS X. For example, the
MySQLdbandPILpackages are frequent stumbling blocks on Mac OS X because of the MySQL client libraries andlibjpeg, but MacPorts makes it much easier to get everything right. But you do have the choice to install things directly using MacPorts (sudo port py26-pil) or viapip(sudo pip install pil) oreasy_install, or you can download the source, and do it yourself (sudo python setup.py ...). And because all of them under the covers use Python’sDistutilsto do the dirty work, all of these methods will generally end up installing a package’s files in the same place for a given Python instance.