Atlantic.Net Blog

How to: Useful pip Commands

Introduction

A good knowledge of how to use the pip command to manage Python packages can distinguish between a jumbled mess of spaghetti code and a nicely organized codebase. This guide will teach you the basics of using Pip effectively so you can spend more time writing and less time wrangling code.

Prerequisites

What is Pip

pip is a package manager for the Python programming language. It provides access to a massive repository of open-source tools and libraries and the ability to manage your proprietary packages. One of the most significant benefits of using a package manager is that it automatically installs the dependencies of any package you install, allowing you to quickly get back to writing some code.

Installation

The good news is that Pip is built into Python versions 2.7.9 or later in the 2.X series and 3.4 or later in the 3.X series. If you are using an older version of Python, you’ll need to upgrade to get Pip natively. However, if that is not possible, it is easy to install by downloading the bootstrap script and running it from the command line like so:

python get-pip.py

Upgrading Pip

Even if you have a modern version of Python with Pip prepackaged, it may be outdated when you read the guide. Fortunately, it has the wonderfully helpful ability to upgrade itself, like so:

pip install -U pip

Or on Windows:

python -m pip install -U pip

The different upgrade command in Windows is necessary because in Windows it is not possible to overwrite a running executable. With this upgrade command, the running executable is actually python.exe, which makes pip.exe available to overwrite.

pip Commands

Now that you have Pip, it’s time to start managing some packages! Simply enter pip into your shell to get a list of pip commands and an overview of available options. Let’s walk through their usage.

pip search

The search command allows you to search through the database of packages in Python’s central package repository, PyPI. For example:

pip search django

This search will generate a list of the over 8,000 packages with the term “Django” in either the package’s name or description. The lookup is case-insensitive so that this search will return results matching “Django” or “DJANGO” (or any other case variation, such as “Django” or even “djAngo”).

pip install

Arguably the most useful command pip provides, the install command installs a Python package and its dependencies into your Python environment. The most straightforward usage is similar: specify the name of the package you wish to install.

pip install Flask

The install command is also case-insensitive. You can also install a specific version of a package or a range of versions. You might want to specify a range if you require a box as a dependency to your own code, and you wish to indicate that your code will work with any version in that range.

pip install Flask==0.10.1
# install this particular version 

pip install "Flask>=0.9,<0.11"
# install versions matching the range from 0.9 up to (but not including) 0.11

You can also put a list of packages (one per line) in a text file and install all of them at once using the -r or --requirement flag.

pip install -r requirements.txt

The install command can also upgrade packages (as illustrated above in Upgrading Pip) by including the -U or --upgrade option. Despite the --upgrade name, the option can be used anytime you need to change the version of an installed package, whether the new version is higher or lower than the installed version.

If the package you wish to install is not in the PyPI repository, then you can install from a version control system. For example:

pip install git+https://github.com/cherrypy/cherrypy.git

pip uninstall

Of course, sometimes you need to remove packages. The uninstall command removes the specified package(s) from your environment.

pip uninstall Pyramid

pip freeze

If building a list of all the packages you need sounds like a lot of work, then the freeze command is for you. This command outputs all the currently installed Python packages and their versions. You can also redirect this output to a text file to build a ready-made source to use with the install command.

pip freeze > requirements.txt

.

Learning more

In this tutorial, we have barely scratched the surface of the feats you can accomplish with Pip. The best way to learn more is to get out there and use Pip to manage your Python project’s dependencies. For reference, you can find the complete pip documentation here.

Thank you for following along with this how-to; we hope you have enjoyed it. Please check back here for more updates or check out the many services offered by Atlantic.Net, including dedicated server hosting, Web Server Hosting, and SQL Hosting.

.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year