1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to Third-Party Package Management in Python

Demonstration of PIP

Start course
Difficulty
Beginner
Duration
51m
Students
9
Description

This content is designed to introduce new developers to managing third-party packages using the Python package index and a tool called Pip. This course is part of a series of content designed to help you learn to program with the Python programming language. 

Learning Objectives

  • The Python Package Index
  • The tool used to manage packages called pip
  • The tool used to create isolated Python environments named venv
  • Considerations to be made before installing a third-party package

Intended Audience

This course was designed for new developers wanting to learn Python. 

Prerequisites

  • That you’re familiar with the Python runtime
  • That you’re familiar with the Python language syntax
  • This course assumes that you’re not yet familiar with how to install and manage third-party Python software
Transcript

The Python package index consists of thousands of third-party packages that we can download, install, and use. The primary tool for downloading and installing is named pip. Which stands for package installer for Python. 

Pip is a module provided by many modern versions of Python. However, depending on the version of Python and how it was installed pip may not be installed on your operating system. We’re not going to review installing pip in the course. The installation instructions are online and maintained with the latest details. 

Once pip is installed you can run pip using the command line. The Python runtime provides a mechanism for calling code inside a module from the command line. We use this mechanism by specifying the minus m (-m) flag when running the Python binary. The minus m flag allows us to specify a module to run as a command line application. 

We’re not going to review how this mechanism works in this course. However, just know that this minus m flag is instructing Python to run some bit of code inside the pip module that behaves like a command line application.

If you run the command python3 -m pip and it doesn’t result in an error, then pip is likely installed. Notice here that calling the pip module displays this list of allowed commands and these command line options.

These commands are used to manage third-party packages. Let’s go through some of the more commonly used commands to help get you started. The list (python3 -m pip list) command is used to display the third-party packages currently installed. 

Notice that I have a few listed here. More specifically notice that there is not a package here named Rich. Let’s use the install command to install the Rich package. This is a third-party package used to make better-looking console-based applications. I’m using this as an example because it’s easy to test in a demo; it also includes multiple versions which will help demonstrate installing specific versions. 

The pip command for installing packages is called install. This one command can download and install a package and all of the packages on which it depends. Let’s review the command line usage info by specifying the minus h flag to the install command. This is a help flag that will display help information for the given context. In this case the context is the install command. 

We’re not going to focus on all of these options in this course. However, it is worth knowing that they exist. By specifying a package name to the install command pip will attempt to locate, download and install the package. 

Let’s use the Python package index website to find the exact name of the package to install. https://pypi.org/project/rich/

Let’s use this copy-feature to copy the install command. Notice that this command listed here doesn’t quite look like the commands used so far. We’ve been calling pip as a module using Python’s minus m flag. However, depending on the install there’s often a script that exists named pip that serves as a sort of proxy for the module. 

I’ll continue to use the Python module syntax going forward however, using this proxy is a viable option. 

Okay let’s install Rich. python3 -m pip install rich

Notice this output here shows how pip is downloading and installing the packages that Rich depends on and then installs Rich. Being able to install all the dependent packages is what makes pip so useful. Because it can ensure that we have all the code needed to use the installed package. 

Okay, now that the package is installed if we re-run the list command we’ll see Rich and its dependencies are now installed. Let’s start up an interactive Python session so that we can use the newly installed package. 

To make it easier on us, let’s copy some code from the package’s introduction documentation. 

from rich import print

print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())

Check out this code here: this code imports a function named print from our newly downloaded package. 

By importing this print function we’re basically replacing the built-in print function with the version provided by rich. By calling this print function it’s going to use the print function provided by Rich. Which can more easily display text in different colors and fonts. 

Compare the results from rich vs the built-in print function. Notice rich adds some nice color to the text and can even display emojis. 

Okay, let’s end this interactive session. We don’t want to focus on what Rich can do. That’s not our goal for this course. Rather, I wanted you to see how easy it is to begin using a third-party package once installed.

Once a package is installed we can use its functionality in our code. 

Let’s see how to uninstall a package using the aptly named uninstall command. The uninstall command expects the name of the package to uninstall. Providing this with the name rich will prompt for confirmation.

Selecting y will continue the process and remove the package. There will be times when you may want to install or remove a package without being prompted. For example in some sort of automated testing. In these cases, you can use the minus y flag which will pre-set the confirmation prompt to yes.

Using the list command shows us that rich is now removed. When we installed rich we used the command pip install rich. Because we didn’t specify a version, pip assumes we want to use the latest version.

Let’s use the install command and install a different version of rich. Pip includes a mechanism to specify which versions we’re willing to install. This mechanism is referred to as requirement specifiers.

By using a requirement specifier pip is able to install specific versions of a package. 

Let’s review the syntax for requirement specifiers.

The only requirement for a  requirement specifier is to provide a package name. If we don’t include anything else then the latest package version is installed. 

One of the features of a package is that a package can include multiple optional components. To access these options there’s a syntax which uses square brackets to specify one or more options. Each option is separated by a coma. 

Once a package is specified with-or-without options we can define one or more version requirements. We can specify one or more version requirements choosing from a list of operators used to compare package versions. 

  • ==

    • For an exact match you can use the matching operator. 

  • !=

    • To exclude a version you can use the exclusion operator.

  • < >

    • To include versions of greater or lesser versions use the exclusive order operator.

  • <= >=

    • To include versions of greater-or-equal or lesser-or-equal value use the inclusive order operator.

  • ~=

    • The compatible version operator is used as a shorthand syntax which compresses multiple version specifiers.

  • ===

    • The arbitrary matching operator is used to match exact strings. All of the rest of these operators expect that a version follows a specific structure. For packages that don’t follow the expected version structure this operator provides a mechanism for matching the text of the version.

When defining multiple version specifiers using commas pip has to meet all of these requirements. These are connected using a logical and operator.

It’s the version specifier syntax that makes it possible for us to install specific versions of a package. And because we can specify which versions to use we can control the rate of external code change. 

What we’ve covered here is just scratching the surface of pip’s full feature set. However, these few commands are possibly among the most commonly used. So knowing how to install a third-party package, how to remove it, and how to see which packages are installed will get you pretty far.

Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:

  • Pip is a Python module included with modern versions of Python.

    • It’s used to manage third-party packages. 

  • Pip can be run on the command line using the module syntax or possibly using the pip proxy script. 

  • Pip consists of a series of commands and options. We covered a few commands including:

    • List - which lists installed packages.

    • Install - which installs packages.

    • Uninstall - which uninstalls packages.

  • Pip allows specific package versions to be selected using version specifiers.

    • Version specifiers have their own syntax to define which versions of a package are acceptable.

Alright, that's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!

About the Author
Students
93998
Labs
28
Courses
45
Learning Paths
54

Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.

Covered Topics