2011 February 19

Introduction to Python packages

So I came across a nice python tool that I wanted to try out but I'm a total python noob. Installed Python on Windows, and then the install instructions for the tool said run pip install .., and thus began my quest to figure out how to install additional packages in Python.

The standard method for publishing Python packages is called Distutils, I understood it as similar to make. However, easy_install and pip are more popular ways to install packages since they provide an ease of use similar to dpkg or gem. These tools typically use the Python Package Index (PyPI) as the online package repository. A simple command will download the library, auto-fetch any dependencies and do any post-installation setup. A related package virtualenv, is used for 'serious' environments, when you have to manage packages across multiple projects; for example when you want to ensure that a project uses a particular version of a library.

So, now my goal was to get pip. A prerequisite for installing it is the setuptools utility, which has the easy_install script. Once that was installed, I could get pip by executing the following on the command line.

easy_install pip

But for completeness, I also wanted to try out the manual distutils method. So I proceeded to uninstall pip. Now I hit a gotcha easy_install seems to be a 'one-way' tool! There is no 'uninstall' command out of the box, it has to be worked around.

So the workaround is to do :

easy_install -mxN pip

which oddly enough, 'installs' pip in multi-version mode, that is, it removes its entry from the easy-install.pth file that had marked the previous installation as the default version. It also helpfully indicates the location of the pip 'egg' directory that can be deleted manually.

Once that is deleted, I downloaded a source package of pip, and ran

python setup.py install

in the unzipped folder. So once again pip is in action, with a pip.exe freshly compiled in the Python\Scripts folder.

Now I could finally start playing with the tool ( and realized that I almost forgot why I started this saga ;) !)

References