Every Python developer must have used pip and easy_install to install Python modules. But it still remains a confusion “What’s the difference, anyhow? “. This blog post is to give you clear understanding of which one to use.
First of all I would like to explain few terms for Python beginners.
- pip – pip is a tool for installing Python packages from the Python Package Index. Starting with Python 3.4, it is included by default with the Python binary installers.
- easy_install – It is a package manager for Python that provides a standard format for distributing Python programs and libraries (based on the Python Eggs format).
- Python Package Index (PyPI) – The official third-party software repository for the Python programming language.
- Setuptools: Setuptools is a package development library designed to facilitate packaging Python projects by enhancing the Python standard library distutils (distribution utilities).
How to install a Python module ?
- The most basic way to install a package is to download the source code and run ‘python setup.py install’ inside the package directory.
#tar -xvzf mechanize-0.2.5.tar.gz
#python setup.py install
- Second way to install module is using easy_install (default Python package manager).
Searching for mechanize
Best match: mechanize 0.2.5
Running mechanize-0.2.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-FiMleV/mechanize-0.2.5/egg-dist-tmp-SKWpzt
Adding mechanize 0.2.5 to easy-install.pth file
Processing dependencies for mechanize
Finished processing dependencies for mechanize
- Third way to install module is using pip
#pip install mechanize
Downloading mechanize-0.2.5.tar.gz (383kB): 383kB downloaded
Running setup.py egg_info for package mechanize
Installing collected packages: mechanize
Running setup.py install for mechanize
Successfully installed mechanize
How pip is better than easy_install ?
Now as a Python developer I would recommend pip over easy_install. To illustrate, I have list down some points which shows how pip is better than easy_install.
- Partial-completed installation:Pip downloads source code and builds it whereas easy_install downloads binaries. This is most crucial point which signifies that partially-completed installation doesn’t occur in case of pip.
Easy_install doesn’t do a whole lot of up-front checking to make sure it’ll actually be able to install both the requested package and the dependencies, and this can lead to problems if something in the dependency chain ends up uninstallable.
Meanwhile, pip looks before it leaps, can bail out early if it’s not going to be able to install your package and will leave behind a useful log file explaining what went wrong.
- Pip requirements file: This is really the killer feature pip has, because it means you can set up a simple plain text file specifying a list of packages (and just as important, which versions of which packages), and get pip to install them all and your project is ready to be used on different machine within seconds.To see how handy this can be, let’s take a simple example:
#pip install requirements.txt
- Integration with virtualenv: Naturally pip integrates quite nicely with virtualenv. Normally, when working in an active virtualenv, Python packaging/installation tools (pip included) will install into that virtualenv, but pip also lets you specify a virtualenv to install into (using the -E flag), and ceate a new virtualenv and install into it.
Let’s take a simple example: suppose you have an application which runs fine against the current Django release (1.0.2), and you want to play around a bit with the Django development trunk to try out a new feature. You could create a requirements file specifying docutils (so the automatic Django documentation will work) and the Subversion URL for Django trunk:
Save this into a file named, say, django-requirements.txt. Then (assuming you have pip and virtualenv installed), run the following:
pip install -E django-trunk -r django-requirements.txt