Luke Hagan

Python's easy_install, pip, and Mac OS X

Put Python packages in your home folder, no sudo required.

Update 9/1/2013: Simplified and updated – works on Mountain Lion!

TL;DR Skip straight to the step-by-step instructions if you’re trying to get Python’s easy_install and pip working without using sudo. If you’re trying to figure out why to use this set of instructions instead of another one, read on.

I’ve been avoiding the use of Python’s easy_install module for, well, as long as I’ve been using Python. Sure, I’ve had to use it here and there but several major things have kept me away all these years:

  1. packages are installed into /Library/Python/X.X/site-packages/, so sudo is required to use it
  2. there’s no need to install these packages system-wide; only I am going to be using them
  3. I really don’t like the idea of installing Python packages in general —- I prefer to just stick the source in my project directory so I don’t have to worry about installing dependencies to use them on other machines

Although I don’t think it’s possible to fully mitigate these issues, I did make some progress this weekend. What follows is the method I should have been using all along, but did not since I failed to read the easy_install documentation. Of course, the documentation assumes you know how to do certain things that aren’t exactly straight forward, and just leaves some out. So here’s a better step-by-step.

The overall strategy is to configure easy_install to put packages into your home folder instead of into /Library. This simultaneously eliminates the need for sudo and the annoyance of installing Python packages system-wide.

Instructions

These instructions assume you have Python 2.7. Depending on your version of OS X, you may have a different version of Python than I’m assuming here. Same goes if you’ve installed your own version of Python instead of using the one that ships with OS X. If you don’t know, check it:

python -V

If the response isn’t 2.7.something, make sure to change any instance of 2.7 in these instructions to match your version (leave out the last digit).

Create a Python site-packages folder in your Library:

mkdir -p ~/Library/Python/2.7/site-packages/

Then, create a bin in your home folder (use another location if you like, but don’t forget to update the path in the upcoming steps as well):

mkdir -p ~/Developer/bin

Create a config file for Python dist utils in your home folder (assuming you’re using TextMate):

mate ~/.pydistutils.cfg

Add the following text to the file and save it:

[install]
install_lib = ~/Library/Python/$py_version_short/site-packages
install_scripts = ~/Developer/bin

Finally, edit your ~/.profile to include your home folder’s bin in PATH and to your new site_packages in PYTHONPATH. Here’s what my .profile looks like; if you’re confused or have never changed your path variable before, look here.

export PATH=/Users/Developer/[your user id]/bin:$PATH
export PYTHONPATH=/Users/[your user id]/Library/Python/2.7/site-packages

Close your Terminal window then re-open it (or just open a new tab) so your shell recognizes the .profle changes. Now just easy_install [whatever] as usual, but without the sudo.

Recommendation: install pip

While easy_install may still be necessary for some things (such as installing pip), pip is supposed to be the future. pip will use the same paths that you just configured for easy_install, so all you have to do is install it and go:

easy_install pip
pip install [whatever]