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!

Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary