pip is a package management system used to install and manage software package for Python. If you have installed Python 3.4 or later, pip is included with Python and should already be working on your system. To install or upgrade pip, securely download get-pip.py. Then run the following (which may require administrator access):

python get-pip.py

This article discuss about various ways to install, update and remove package using pip in Python.

Install Package

Once pip is installed, it can be used to install python packages using below command on command prompt.

# Install package traitlets
python -m pip install traitlets

Sometimes we want to install packages for specific version of Python. Below command shows how to install packages for Python2.6 and Python2.7.

# Install a module using pip for python 2.6
python2.6 -m pip install beautifulsoup4

# Install a module using pip for python 2.7
python2.7 -m pip install beautifulsoup4

Below example illustrate how to install specific version of packages.

pip install -Iv MySQL_python==1.2.2

In above example options mean:

  • -I stands for --ignore-installed which will ignore the installed packages, overwriting them.
  • -v is for verbose. You can combine for even more verbosity up to 3 times (e.g. -Ivvv).

If the package is already installed and you want to downgrade it add --force-reinstall like this:

pip install 'stevedore>=1.3.0,<1.4.0' --force-reinstall

pip also supports multiple packages in one go. Put your modules to install in a file requirements.txt with one item per line. After that execute below command

# Content of requirements.txt

# Install modules
pip install -r requirements.txt

If you want to copy packages in a particular environment, do this

# Original environment
pip freeze >> requirements.txt

# Run on different machine (cloned environement) then above.
pip install -r requirements.txt

In other words pip freeze >> requirements.txt shows packages YOU installed via pip. Output format, freeze gives us the standard requirement format that may be used later with pip install -r to install requirements from. Later on a different machine or on a clean environment, after executing pip install -r requirements.txt, you’ll get the an identical environment with the exact same dependencies installed as you had in the original environment.

Upgrade Package

Below command will upgrade an existing package setuptools (or distribute), to the latest version

# Short command
pip install setuptools -U

# Long command
pip install setuptools --upgrade

We also update packages using python. Below command upgrade the pip package.

python -m pip install --upgrade pip

To upgrade all local packages, you can install pip-review. After that, you can either upgrade the packages interactively or automatically.

# Install pip-review
$ pip install pip-review

# Upgrade the packages interactively
$ pip-review --local --interactive

# Upgrade the packages automatically:
$ pip-review --local --auto

Uninstall Package

Below command shows how to uninstall a package.

python -m pip uninstall traitlets
pip uninstall traitlets

Above example will uninstall package traitlets. It doesn’t uninstall the dependencies packages. It only removes the specified package. You can install and use the pip-autoremove utility to remove a package plus unused dependencies.

# Install pip-autoremove
pip install pip-autoremove

# Remove "traitlets" plus its unused dependencies:
pip-autoremove traitlets -y

Following example shows how to remove all the modules installed using pip.

# List of package installed using pi
pip freeze > requirements.txt

# Remove one by one
pip uninstall -r requirements.txt

# Remove all at once
pip uninstall -r requirements.txt -y

In the above example, first we get the list of all pip packages in the requirements.txt (this will overwrite requirements.txt if exist else will create the new one). Next we uninstall the modules.