A short tutorial on Python packaging

If you are a beginner to Python, then you might find it confusing that there are several ways to install Python packages and which one to use. They are:

  1. distutils – With distutils you would typically download the package, untar it and then run:
     $ python setup.py install

    This will install the library files (packages and modules) into the ‘site-packages’ directory of the Python interpreter. On my ubuntu11.04 the location of site-packages is /usr/local/lib/python2.6/site-packages/. Note that distutils does not install dependencies.

     

  2. easy_install (part of setuptools package) – It automates the process by downloading a named package from PyPi (which is a repository of Python Packages). To install packages using easy_install run:
    $ easy_install <package-name>

    and it downloads and installs the package in ‘dist-packages’ directory of the Python interpreter.

     

  3. pip – succeeds easy_install as it has several advantages. A few of them are:
    • support for uninstallation of packages, which is not possible with easy_install
    • installs dependencies
    • partial / incomplete installation do not happen with pip
    • pip automatically installs all packages specified in a file with pip install -r requirements.txt

There are other benefits of pip which are listed here: http://stackoverflow.com/questions/3220404/why-use-pip-over-easy-install

To install packages using pip simply run:

$ pip install <package_name>

or to install all packages listed in a file run:

$ pip install -r requirements.txt

To see the list of installed packages run:

$ pip freeze

Apart from the tools listed above there are several others which succeed like disutils2 and distribute but they are not used very commonly.

Summary

Pip is the future and is the recommended way to install python packages. For best results use pip with virtualenv. I will talk about virtualenv in another post of mine in this series.

Advertisements