@Ramana's worked the best for me, of those here, but I had to add a few catches:
for dist in pip.get_installed_distributions():
if 'site-packages' in dist.location:
pip.call_subprocess(['pip', 'install', '-U', dist.key])
except Exception, exc:
site-packages check excludes my development packages, because they are not located in the system site-packages directory. The try-except simply skips packages that have been removed from PyPI.
@endolith: I was hoping for an easy
pip.install(dist.key, upgrade=True), too, but it doesn't look like pip was meant to be used by anything but the command line (the docs don't mention the internal API, and the pip developers didn't use docstrings).