What Is Pip? A Guide for New Pythonistas

What is pip? pip is the standard package manager for Pyhon. It allows you to install and manage additional packages that are not part of the Python standard library. This tutorial is an introduction to pip for new Pythonistas.

In this tutorial, you’ll learn about:

  • Installing additional packages not included with the standard Python distribution
  • Finding packages published to the Python Package Index (PyPI)
  • Managing requirements for your scripts and applications
  • Uninstalling packages and their dependencies

As you’ll see, the Python community is very active and has created some neat alternatives to pip that you’ll learn about later in this tutorial.

Getting Started With pip

So, what is pip? pip is a package manager for Python. That means it’s a tool that allows you to install and manage additional libraries and dependencies that are not distributed as part of the standard library.

Package management is so important that pip has been included with the Python installer since versions 3.4 for Python 3 and 2.7.9 for Python 2, and it’s used by many Python projects, which makes it an essential tool for every Pythonista.

The concept of a package manager might be familiar to you if you are coming from other languages. JavaScript uses npm for package management, Ruby uses gem, and .NET use NuGet. In Python, pip has become the standard package manager.

The Python installer installs pip, so it should be ready for you to use, unless you installed an old version of Python. You can verify that pip is available by running the following command in your console:

$ pip --version

pip 18.1 from C:Python37libsite-packagespip (python 3.7)

You should see a similar output displaying the pip version, as well as the location and version of Python. If you are using an old version of Python that does not include pip, then you can install it by following the instructions for your system in the pip installation documentation.

You probably want to follow the examples in this tutorial inside a virtual environment to avoid installing packages to the global Python installation. You can learn about virtual environments in Python Virtual Environments: A Primer. The Using Virtual Environments section of that article explains the basics of creating new virtual environments.

Installing Packages With pip

Python is considered a batteries included language. This means that the Python standard library includes an extensive set of packages and modules to help developers with their scripts and applications.

At the same time, Python has a very active community that contributes an even bigger set of packages that can help you with your development needs. These packages are published to the Python Package Index, also known as PyPI (pronounced Pie Pea Eye). PyPI hosts an extensive collection of packages that include development frameworks, tools, and libraries.

Many of these packages simplify Python development by providing friendly interfaces to functionality that already exists in the standard library. For example, you can write a script that retrieves the contents of a web page using only the standard libraries included with Python:

# In using-http.py

import cgi
import http.client

server = 'www.google.com'
url = '/'
conn = http.client.HTTPSConnection(server)
conn.request('GET', url)
response = conn.getresponse()
content_type = response.headers.get('Content-Type')
_, params = cgi.parse_header(content_type)
encoding = params.get('charset')
data = response.read()
text = data.decode(encoding)

print(f'Response returned: {response.status} ({response.reason})')
print('Body:')
print(text)

In this script, you import cgi and http.client, both of which are included in the Python standard library. You create an HTTPSConnection object specifying the server and invoke its .request() and .getresponse() to retrieve a response.

From the response, you can retrieve the Content-Type header and parse it using the cgi module to extract the charset in which the page is encoded.

cgi.parse_header() returns a tuple with a main value and a dictionary of parameters. For example, the Content-Type header might contain a value like text/html; charset=ISO-8859-1.

The tuple will contain the string text/html as the first element, and the second element will be a dictionary in the form {'charset': 'ISO-8859-1'}. Because you only care about the charset parameter, you can ignore the beginning of the tuple using an underscore: _, params = cgi.parse_header(content_type).

Once you have the encoding of the page, you can read the response and decode it into text. You can run the example in the console to see how it works:

$ python using-http.py

Response returned: 200 (OK)
Body:
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" 
lang="en"><head><meta content="Search the world's information, including 
webpages, images, videos and more. Google has many special features to help you 
find exactly what you're looking for." name="description"><meta content="noodp" 
name="robots">... Additional Output Omitted

This seems like a lot of work for a small script that retrieves the contents of a web page. Fortunately, there is a Python package that simplifies HTTP requests and provides a nice interface to do exactly what you want.

Basic Package Installation

PyPI hosts a very popular library to perform HTTP requests called requests. You can learn all about it in its official documentation site.

The first step is to install the requests package into your environment. You can learn about pip supported commands by running it with help:

$ pip help

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible 
                              dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment 
                              variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be 
                              used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be 
                              used up to 3 times (corresponding to WARNING, 
                              ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form 
                              [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should 
                              attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: 
                              (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort).
  --trusted-host <hostname>   Mark this host as trusted, even though it does 
                              not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file 
                              containing the private key and the certificate in 
                              PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine 
                              whether a new version of pip is available for 
                              download. Implied with --no-index.

As you can see, pip provides an install command to install packages. You can run it to install the requests package:

$ pip install requests

Looking in indexes: https://pypi.org/simple
Collecting requests
  Using cached 
  https://files.pythonhosted.org/packages/7d/e3/
  20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/
  requests-2.21.0-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Using cached https://files.pythonhosted.org/packages/bc/a9/
  01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/
  chardet-3.0.4-py2.py3-none-any.whl
Collecting idna<2.9,>=2.5 (from requests)
  Using cached https://files.pythonhosted.org/packages/14/2c/
  cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/
  idna-2.8-py2.py3-none-any.whl
Collecting urllib3<1.25,>=1.21.1 (from requests)
  Using cached https://files.pythonhosted.org/packages/62/00/
  ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/
  urllib3-1.24.1-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
  Using cached https://files.pythonhosted.org/packages/9f/e0/
  accfc1b56b57e9750eba272e24c4dddeac86852c2bebd1236674d7887e8a/
  certifi-2018.11.29-py2.py3-none-any.whl
Installing collected packages: chardet, idna, urllib3, certifi, requests
Successfully installed certifi-2018.11.29 chardet-3.0.4 idna-2.8 
  requests-2.21.0 urllib3-1.24.1
You are using pip version 18.1, however version 19.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' 
  command.

You should see an output similar to the one above. You use pip with an install command followed by the name of the package you want to install. pip looks for the package in PyPI, calculates its dependencies, and installs them to insure requests will work.

You can also see that the current environment is using pip version 18.1, but version 19.0.1 is available. It also shows the command you should use to update pip, so let’s do that:

$ python -m pip install --upgrade pip

Looking in indexes: https://pypi.org/simple
Collecting pip
  Downloading https://files.pythonhosted.org/packages/46/dc/
  7fd5df840efb3e56c8b4f768793a237ec4ee59891959d6a215d63f727023/
  pip-19.0.1-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 2.0MB/s
Installing collected packages: pip
  Found existing installation: pip 18.1
    Uninstalling pip-18.1:
      Successfully uninstalled pip-18.1
Successfully installed pip-19.0.1

Notice that you use python -m to update pip. The -m switch tells Python to run a module as an executable. This is necessary because in order for you to update pip, the old version has to be uninstalled before installing the new version, and removing it while running the tool can cause errors.

When you run pip as a module, Python loads the module in memory and allows the package to be removed while it is being used. You can run packages as if they were scripts if the package provides a top-level script __main__.py.

Now that you have installed requests and upgraded pip, you can use the list command to see the packages installed in your environment:

$ pip list

Package    Version
---------- ----------
certifi    2018.11.29
chardet    3.0.4
idna       2.8
pip        19.0.1
requests   2.21.0
setuptools 40.6.2
urllib3    1.24.1

As you can see, pip has been upgraded to version 19.0.1 (the latest version at the moment), and requests version 2.21.0 has been installed.

The pip install <package> command always looks for the latest version of the package and installs it. It also searches for dependencies listed in the package metadata and installs those dependencies to insure that the package has all the requirements it needs.

As you can see, multiple packages were installed. You can look at the package metadata by using the show command in pip:

$ pip show requests

Name: requests
Version: 2.21.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: py37libsite-packages
Requires: certifi, chardet, idna, urllib3
Required-by:

The metadata lists certifi, chardet, idna, and urllib3 as dependencies, and you can see they were also installed.

With the requests package installed, you can modify the example above and see how easy it is to retrieve the contents of a web page:

# In using-requests.py

import requests

url = 'https://www.google.com'
response = requests.get(url)
print(f'Response returned: {response.status_code}, {response.reason}')
print(response.text)

You can import the requests package as any other standard package because it is now installed in your environment.

As you can see, requests.get() handles the HTTP connection for you and returns a response object similar to the original example but with some interface improvements.

You don’t have to deal with the encoding of the page because requests will handle that for you in most situations. Still, requests provides a flexible interface to handle special cases through the requests.Response object.

Using Requirement Files

The pip install command always installs the latest published version of a package, but sometimes, you may want to install a specific version that you know works with your code.

You want to create a specification of the dependencies and versions you used to develop and test your application, so there are no surprises when you use the application in production.

Requirement files allow you to specify exactly which packages and versions should be installed. Running pip help shows that there is a freeze command that outputs the installed packages in requirements format. You can use this command, redirecting the output to a file to generate a requirements file:

$ pip freeze > requirements.txt
$ cat requirements.txt

certifi==2018.11.29
chardet==3.0.4
idna==2.8
requests==2.21.0
urllib3==1.24.1

The freeze command dumps all the packages and their versions to standard output, so you can redirect the output to a file that can be used to install the exact requirements into another system. The convention is to name this file requirements.txt, but you can give it any name you want.

When you want to replicate the environment in another system, you can run pip install specifying the requirements file using the -r switch:

$ pip install -r requirements.txt

Looking in indexes: https://pypi.org/simple
Collecting certifi==2018.11.29 (from -r .requirements.txt (line 1))
  Using cached https://files.pythonhosted.org/packages/9f/e0/
  accfc1b56b57e9750eba272e24c4dddeac86852c2bebd1236674d7887e8a/
  certifi-2018.11.29-py2.py3-none-any.whl
Collecting chardet==3.0.4 (from -r .requirements.txt (line 2))
  Using cached https://files.pythonhosted.org/packages/bc/a9/
  01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/
  chardet-3.0.4-py2.py3-none-any.whl
Collecting idna==2.8 (from -r .requirements.txt (line 3))
  Using cached https://files.pythonhosted.org/packages/14/2c/
  cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/
  idna-2.8-py2.py3-none-any.whl
Collecting requests==2.21.0 (from -r .requirements.txt (line 4))
  Using cached https://files.pythonhosted.org/packages/7d/e3/
  20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/
  requests-2.21.0-py2.py3-none-any.whl
Collecting urllib3==1.24.1 (from -r .requirements.txt (line 5))
  Using cached https://files.pythonhosted.org/packages/62/00/
  ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/
  urllib3-1.24.1-py2.py3-none-any.whl
Installing collected packages: certifi, chardet, idna, urllib3, requests
Successfully installed certifi-2018.11.29 chardet-3.0.4 idna-2.8 
  requests-2.21.0 urllib3-1.24.1

The versions of the packages will match those listed in requirements.txt:

$ pip list

Package    Version
---------- ----------
certifi    2018.11.29
chardet    3.0.4
idna       2.8
pip        19.0.1
requests   2.21.0
setuptools 40.6.2
urllib3    1.24.1

You can submit the requirements.txt file into source control and use it to create the exact environment in other machines.

Fine-Tuning Requirements

The problem with hardcoding the versions of your packages and their dependencies is that packages are updated frequently with bug and security fixes, and you probably want to leverage those as soon as they are published.

The requirements file format allows you to specify dependency versions using logical operators that give you a bit of flexibility to insure packages are updated, but still define the base versions of a package.

Open the requirements.txt file in your favorite editor and make the following changes:

certifi>=2018.11.29
chardet>=3.0.4
idna>=2.8
requests>=2.21.0
urllib3>=1.24.1

You can change the logical operator to >= to tell pip to install an exact or greater version that has been published. When you set a new environment using the requirments.txt file, pip looks for the latest version that satisfies the requirement and installs it. You can upgrade the packages in your requirements file by running the install command with the --upgrade switch:

$ pip install --upgrade -r requirements.txt

Looking in indexes: https://pypi.org/simple
Requirement already up-to-date: certifi==2018.11.29 in py37libsite-packages 
  (from -r .requirements.txt (line 1)) (2018.11.29)
Requirement already up-to-date: chardet==3.0.4 in py37libsite-packages 
  (from -r .requirements.txt (line 2)) (3.0.4)
Requirement already up-to-date: idna==2.8 in py37libsite-packages 
  (from -r .requirements.txt (line 3)) (2.8)
Requirement already up-to-date: requests==2.21.0 in py37libsite-packages 
  (from -r .requirements.txt (line 4)) (2.21.0)
Requirement already up-to-date: urllib3==1.24.1 in py37libsite-packages 
  (from -r .requirements.txt (line 5)) (1.24.1)

Nothing was upgraded because you have the latest versions, but if a new version was published for a listed package, then the package would’ve been upgraded.

In an ideal world, new versions of packages would be backwards compatible and would never introduce new bugs. Unfortunately, new versions can introduce changes that will break your application. The requirements file syntax supports additional version specifiers to fine-tune your requirements.

Let’s say that a new version 3.0 of requests is published but introduces an incompatible change that breaks your application. You can modify the requirements file to prevent 3.0 or higher from being installed:

certifi>=2018.11.29
chardet>=3.0.4
idna>=2.8
requests>=2.21.0, <3.0
urllib3>=1.24.1

Changing the version specifier for the requests package ensures that any version greater or equal to 3.0 does not get installed. The pip documentation provides all the information about the requirements file format, and you can consult it to learn more about it.

Production vs Development Dependencies

Not all packages that you install during the development of your applications are going to be application dependencies. There are many packages published to PyPI that are development tools or libraries that you want to leverage during the development process.

As an example, you’ll probably want to unit test your application, so you need a unit test framework. A popular framework for unit testing is pytest. You want to install it in your development environment, but you do not want it in your production environment because it isn’t an application dependency.

You create a second requirements file (requirements_dev.txt) to list additional tools to set up a development environment:

# In requirements_dev.txt
pytest>=4.2.0

This requires you to use pip to install both requirement files: requirements.txt and requirements_dev.txt. Fortunately, pip allows you to specify additional parameters within a requirements file. You can modify requirements_dev.txt to also install the requirements from the production requirements.txt file:

# In requirements_dev.txt
-r requirements.txt
pytest>=4.2.0

Notice that you are using the exact same -r switch to install the production requirements.txt file. The requirements file format allows you to specify additional arguments right on a requirements file.

Freezing Requirements for Production

You created the production and development requirement files and added them to source control. The files use flexible version specifiers to ensure that you leverage bug fixes published by your dependencies. You are also testing your application and are ready to deploy it to production.

You probably want to ensure that the versions of the dependencies you deploy to production are the exact same versions you used in your integration pipeline or build process because you know all the tests pass and the application works.

The current version specifiers don’t guarantee that the same versions will be deployed to production, so you want to freeze the production requirements as you saw earlier.

You create a clean production virtual environment and install the production requirements using the requirements.txt file. Once the requirements are installed, you can freeze the specific versions, dumping the output to a requirements_lock.txt file that you use in production. The requirements_lock.txt file will contain exact versions specifiers and can be used to replicate the environment.

Finding Packages to Use

As you become a more experienced Pythonista, there’ll be a set of packages that you’ll know by heart and that you’ll use in most of your applications. The requests and pytest packages are good candidates to become useful tools in your Python toolbox.

There will be times though when you will need to solve a different problem, and you will want to look for a different tool or library that can help you with it. As you can see above, pip help shows that there is a search command that looks for packages published to PyPI.

Let’s see how this command can help us:

$ pip help search

Usage:
  pip search [options] <query>

Description:
  Search for PyPI packages whose name or summary contains <query>.

Search Options:
  -i, --index <url>           Base URL of Python Package Index 
                              (default https://pypi.org/pypi)

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment 
                              variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be 
                              used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be 
                              used up to 3 times (corresponding to WARNING, 
                              ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form 
                              [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should 
                              attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: 
                              (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort).
  --trusted-host <hostname>   Mark this host as trusted, even though it does 
                              not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file 
                              containing the private key and the certificate in 
                              PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine 
                              whether a new version of pip is available for 
                              download. Implied with --no-index.
  --no-color                  Suppress colored output

The command takes a set of options listed above and a <query>. The query is just a string to search for and will match packages and their descriptions.

Let’s say your application needs to access a service that is using OAuth2 for authorization. Ideally, there is a library that works with requests or with a similar interface that can help us. Let’s search PyPI for it using pip:

$ pip search requests oauth

requests-oauth (0.4.1)             - Hook for adding Open Authentication 
                                     support to Python-requests HTTP library.
oauth (1.0.1)                      - Library for OAuth version 1.0a.
pmr2.oauth (0.6.1)                 - OAuth PAS Plugin, OAuth 1.0 provider for 
                                     Plone.
oauth-proxy (1.0.5)                - OAuth HTTP proxy
django-oauth (1.1)                 - Support of OAuth in Django.
intuit-oauth (1.2.0)               - Intuit OAuth Client
brubeck-oauth (0.1.11)             - Brubeck OAuth module
guillotina-oauth (2.0.0)           - guillotina oauth support
httpie-oauth (1.0.2)               - OAuth plugin for HTTPie.
paytm-oauth (0.2)                  - Consumer for paytm oauth
plurk-oauth (0.9.2)                - Plurk OAuth API
oauth-flow (1.0.3)                 - Authenticate and make calls to OAuth 1.0, 
                                     OAuth 2.0 services
... Additional Output Omitted

The search term yields quite an extensive collection of packages. Some of them seem specific to a service or technology like django-oauth. Others look promising, like requests-oauth. Unfortunately, there isn’t much information other than a brief description.

Most of the time, you want to search for packages directly in the PyPI website. PyPI provides search capabilities for its index and a way to filter results by the metadata exposed in the package, like framework, topic, development status, and so on.

A search for the same terms in PyPI yields a lot of results, but you can filter them by different categories. For example, you can expand the Intended Audience and select Developers since you want a library that helps you with developing your application. Also, you probably want a package that is stable and production-ready. You can expand the Development Status category and select Production/Stable:

PyPi Search Results for Requests OAuth

You can apply additional filters and tweak the search terms until you find the package that you are looking for.

The results provide a link to the package page, which contains more information and hopefully some documentation. Let’s take a look at the information for requests-oauth2:

PyPi Package Page for Requests OAuth 2

The project page provides more information, and it seems to have a link to the project homepage. The link takes you to the project repository on GitHub. There, you can see some more information about the project and some usage examples.

Finding the original source code repository can be an invaluable resource. There, you can find some hints about the status of the project by looking at the date of the latest commits, number of pull request and open issues, and so forth.

Another option to find a package is to Google it. Widely used Python libraries will show up at the top of google searches, and you should be able to find a link to the package in PyPI or its source code repository.

Finding the right package may take some time and research, but it will also speed up your development process once you find it.

Uninstalling Packages

Once in a while, you will have to uninstall a package. You either found a better library to replace it, or it is something you don’t really need. Uninstalling packages can be a bit tricky.

Notice that, when you installed requests, pip installed other dependencies too. The more packages you install, the bigger the chances that multiple packages depend on the same dependency. This is where the show command in pip comes in handy.

Before you uninstall a package, make sure you run the show command for that package:

$ pip show requests

Name: requests
Version: 2.21.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: c:usersisaacprojectsvirtualenvpy37libsite-packages
Requires: urllib3, certifi, chardet, idna
Required-by:

Notice the last two fields Requires and Required-by. The show command tells us that requests requires urllib3, certifi, chardet, and idna. You probably want to uninstall those two. You can also see that requests is not required by any other package, so it is safe to uninstall it.

You should run the show command against all of the requests dependencies to make sure that no other libraries also depend on them. Once you understand the dependency order of the packages you want to uninstall, you can remove them using the uninstall command:

$ pip uninstall certifi

Uninstalling certifi-2018.11.29:
  Would remove:
    py37libsite-packagescertifi-2018.11.29.dist-info*
    py37libsite-packagescertifi*
Proceed (y/n)? y
  Successfully uninstalled certifi-2018.11.29

Uninstalling a package shows you the files that will be removed and will ask for confirmation. If you are sure you want to remove the package because you’ve checked its dependencies and know that nothing else is using it, you can pass a -y switch to suppress the file list and confirmation:

$ pip uninstall urllib3 -y

Uninstalling urllib3-1.24.1:
  Successfully uninstalled urllib3-1.24.1

$ pip uninstall chardet -y

Uninstalling chardet-3.0.4:
  Successfully uninstalled chardet-3.0.4

$ pip uninstall idna -y

Uninstalling idna-2.8:
  Successfully uninstalled idna-2.8

$ pip uninstall requests -y

Uninstalling requests-2.21.0:
  Successfully uninstalled requests-2.21.0

You can specify all the packages you want to uninstall in a single call: pip uninstall -y urllib3 chardet idna requests.

You can also uninstall all the packages listed in a requirements file by providing the -r <requirments file> option. The command will ask confirmation for each individual package, but you can suppress it, if you know what you’re doing, with the -y switch:

$ pip uninstall -r requirements.txt -y

Uninstalling certifi-2018.11.29:
  Successfully uninstalled certifi-2018.11.29
Uninstalling chardet-3.0.4:
  Successfully uninstalled chardet-3.0.4
Uninstalling idna-2.8:
  Successfully uninstalled idna-2.8
Uninstalling requests-2.21.0:
  Successfully uninstalled requests-2.21.0
Uninstalling urllib3-1.24.1:
  Successfully uninstalled urllib3-1.24.1

Remember to always check the dependencies of packages you want to uninstall. You probably want to uninstall all its dependencies, but uninstalling a package that is being used by others will break your application.

Alternatives to pip

pip is an essential tool for all Pythonistas, and it is used by many applications and projects for package management. This tutorial has helped you with the basics, but the Python community is very active in providing great tools and libraries for other developers to use. These include other alternatives to pip that try to simplify and improve package management.

In this section, you’ll learn about other package management tools available for Python.

Conda Does It All

Conda is a package, dependency, and environment manager for many languages including Python. In fact, its origin comes from Anaconda, which started as a data science package for Python.

Conda is widely used for data science and machine learning applications, and uses its own index to host compatible packages.

Conda not only allows you to manage package dependencies, but it also manages virtual environments for your applications, installs compatible Python distributions, and packages your application for deployment to production.

Setting Up Python for Machine Learning on Windows is a great introduction to Conda that explores package and environment management. The only Windows-specific information is around installation, so it’s still relevant if you use a different OS platform.

Pipenv

Pipenv is another package management tool that “aims to bring the best of all packaging worlds” to Python. It’s gaining a lot of traction among the Python community because it merges virtual environment and package management in a single tool.

It also solves some of the most common hiccups you will run into when manually managing dependencies through pip, like versions of packages, separating development and production dependencies, and locking versions for production.

Pipenv: A Guide to the New Python Packaging Tool is a great start to learn about Pipenv and its approach to package management. Even though the article is tagged as intermediate, the author does a great job of guiding the reader that the article is accessible to anyone starting with Python.

Poetry

Poetry is another pip alternative that is gaining a lot of traction. Like Pipenv, it simplifies package version management and separates development vs production dependencies, and it works by isolating those dependencies into a virtual environment.

If you are coming from JavaScript and npm, then Poetry will look very familiar. It goes beyond package management, helping you build distributions for your applications and libraries and deploying them to PyPI. How to Publish an Open-Source Python Package to PyPI has a good introduction to Poetry and can help you get started.

Conclusion: What Is pip?

This tutorial answered the question, what is pip? You’ve seen that pip is a package manager for Python, used by many projects to manage dependencies. It’s included with the Python installer, which makes it an essential tool for all Pythonistas to know how to use.

Python provides an extensive standard library suitable for developing all sorts of applications, but the active Python community provides an even larger set of tools and libraries that speed up Python application development.

These tools and libraries are published to the Python Package Index (PyPI), and pip allows developers to install them in their application environments.

In this tutorial, you’ve learned about:

  • Installing new packages using pip in the command line and with requirement files
  • Managing dependencies, separating development and production requirements, and creating a locked requirements file
  • Finding packages through pip and PyPI
  • Evaluating package dependencies before uninstalling a package and how pip uninstalls packages

In addition, you’ve learned about the importance of keeping dependencies up to date and alternatives to pip that can help you manage those dependencies.

Feel free to reach out in the comments section below with any questions you might have, and you can always get more information at the pip documentation page.


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]