Python 101: pip – a replacement for easy_install

Pip Installs Python or pip is a tool for installing and managing Python packages, many of which are on the Python Package Index (PyPI). It is a replacement of easy_install. In this article, we’ll spend a little time trying out pip to see how it works and how it might help us in our Python endeavors.

Installation

You will need to go out and get distribute or setuptools to make pip work. If you’re on Python 3, then distribute is your only option since setuptools doesn’t support it at the time of this writing. There’s an installer on pip’s website that you can use called get-pip.py or you can just go to PyPI and download it there as source.

Hopefully you already know this, but to install most modules from source, you have to upzip it and then open a terminal or command line window. Then change directory (cd) to the unpacked folder and run “python setup.py install” (minus the quotes). Note that you may need elevated privileges to install it (i.e. root or administrator). The pip website recommends using pip from within a virtualenv since it is installed automatically and “does not require root access or modify your system Python installation”. I’ll leave that up to your discretion.

pip usage

The most common usage for pip is to install, upgrade or uninstall a package. This is all covered on the pip website, but we’ll look at it here anyway. Since we keep mentioning virtualenv, let’s try installing that with pip:

pip install virtualenv

If you run the command above in a terminal, you should get output similar to this:

Downloading/unpacking virtualenv
  Downloading virtualenv-1.7.2.tar.gz (2.2Mb): 2.2Mb downloaded
  Running setup.py egg_info for package virtualenv
    warning: no previously-included files matching '*' found under directory 'do
cs\_templates'
    warning: no previously-included files matching '*' found under directory 'do
cs\_build'
Installing collected packages: virtualenv
  Running setup.py install for virtualenv
    warning: no previously-included files matching '*' found under directory 'do
cs\_templates'
    warning: no previously-included files matching '*' found under directory 'do
cs\_build'
    Installing virtualenv-script.py script to C:\Python26\Scripts
    Installing virtualenv.exe script to C:\Python26\Scripts
    Installing virtualenv.exe.manifest script to C:\Python26\Scripts
    Installing virtualenv-2.6-script.py script to C:\Python26\Scripts
    Installing virtualenv-2.6.exe script to C:\Python26\Scripts
    Installing virtualenv-2.6.exe.manifest script to C:\Python26\Scripts
Successfully installed virtualenv
Cleaning up...

Well that seemed to work. Note that pip downloads the package BEFORE it start installing it, something that easy_install doesn’t do (for other differences, see this comparison). Say a new version of virtualenv comes out after this article is written and you want to upgrade it? Pip has you covered!

pip install --upgrade virtualenv

Wasn’t that easy? On the other hand, what if you enjoy always working on the bleeding edge outside of the safety of the sandbox? Well uninstalling the sandbox is just as easy as installing it:

pip uninstall virtualenv

Yup. It’s that easy. Really!

Pip can also install from file paths, URLs and version control systems such as Subversion, Git, Mercurial and Bazaar. See the documentation for more information.

Other pip Features

Pip also gives you the ability to create config files that can hold all the command line options you normally use in an INI file-like format. You can read about it here. Sadly it looks like pip ONLY looks for the config in a specific location, so you can’t actually pass different configs to pip.

The other feature I want to highlight is its concepts of requirements files. These files are just lists of packages to install. They provide a way to install a package and all the dependencies, including specific versions of the dependencies. You can even add a list of optional libraries and support tools. If you need to know what your current setup has installed, you can “freeze” your them into a requirements file by doing something like this:

pip freeze > myrequirements.txt

This is most helpful in a virtualenv as you’ll likely have LOTS of packages installed in your main Python suite that don’t have any bearing on your current project. Which is another reason why working with virtualenv might be a good idea.

Wrapping Up

Now you know enough to get started using pip. It’s a pretty handy tool to add to your kit and makes installing and managing packages a breeze. Have fun!

17 thoughts on “Python 101: pip – a replacement for easy_install”

  1. Sounds easy on paper. My own experience is rather different tho. I tried to install Python 2.7 64-bit on a Windows machine with some well knowen packages (NumPy, SciPy) and failed miserably. Some packages or their dependencies just wouldnt install with pip or easy_install. If it weren’t for Mr. Gohlke’s fine collection of compiled windows binaries, i guess i couldn’t have done it at all. I’m new to Python, that’s definately a factor, but i didn’t expect it to be that of a headache. I guess there is a reason people pay for distributions like Enthought.

  2. Hi,
    for windows users, pip is not a option until it will not support binary packages. I know its on pip todo list but until it’s not implemented, the easy_install is practically only option.
    Regards
    Roman

  3. Pingback: Pip 101 [Link] « Macdrifter

  4. forgive my naivete but i frequently see references in python articles to virtualenv but dont really understand what it does/when you should/should not use it….

  5. That’s okay. I’ve been planning to write an article about it, hopefully sometime this week. In the mean time you can check out http://www.virtualenv.org/en/latest/index.html. Basically, you use virtualenv when you want to test out a new package and you don’t want it in your main Python packages. It’s a testbed really. You can test new versions of dependencies to see if they break your app.

  6. Thanks, I’ll check it out. That’s basically what I’d taken away at this point but interested to know more. Looking forward to seeing your take on it in the future.

  7.  Especially for numpy and scipy you do need to have also intalled and set up a compiler (I think a C and a Fortran one).

  8. I wish people would stop touting pip as a replacement for easy_install. Until pip supports binary packages, it’s just as broken as that which it seeks to replace. Having an uninstall capability is really not that compelling.

  9. I honestly use them both, although I think I use easy_install the most just because I’ve used it longer

  10. Pingback: Visto nel Web – 36 « Ok, panico

  11. ‘pip’ failed on me, but ‘easy_install’ saved the day..i don’t know why,.. but ‘pip’ just failed. I tried ‘easy_install’ and it worked like a charm!!

  12. Were you trying to install an egg file? If so, pip is not compatible with eggs. I don’t know why. Anyway, easy_install is the way to go with eggs. Go with pip for most anything else.

  13. i don’t know what those are or why do i need to know ahead of time what format is a module is.Isn’t it these are downloaded from the web?I was on a zLinux mainframe during that.

Comments are closed.