Skip to main content
Enchanted Code

What Is Pip?

tutorial, python, pip

Intro

This tutorial is aimed to provide basic knowledge of Python’s pip interface. It will teach you how to install, upgrade, remove and list Python packages.

Requirements

  • Have Python 3 installed with pip
  • Know how to use the terminal/command-prompt

What Is It?

Pip is a tool that is built-in to Python that allows us to install extra Python packages that are not part of the standard library. External packages can be quite useful as they allow us to use code written by anyone and use it in any of our projects, which can reduce the time required to make a project. As an example flask is a popular package for creating a Python controlled websites.

When a package is created we can specify what requirements we need for that package so a package can be dependent on another package. So don’t be surprised that when installing a package you end up with packages you didn’t specifically install.

Creating packages is not shown in this tutorial

Add Package

We can add packages using the install command. Which is shown below.

You may need to change “python” to be “python3” or something similar depending on your install

python -m pip install <package name>

If installing multiple packages we can install them all at once by simply specifying them one after the other as shown below.

python -m pip install <package 1> <package 2> <package 3>

When installing a package using pip by default packages are installed from PyPI which stands for the “Python Package Index”.

Requirements File

Pip also allows us to install packages listed in a requirements file. This allows us to keep a log of all the packages that were required for a project and then install them back at any time.

We could also give this file to other people

python -m pip install -r requirements.txt

As you can see from the above command we have used the -r flag to show we want to read from a file, then we have listed the filename that we want to use.

A requirements file could look something similar as shown below. As you can see we separate each package name on a new line.

quart
quart-auth
tortoise-orm
pydantic[dotenv]

You might see the square brackets on one of the requirements, this is an optional requirement we can install with that package, these are not being covered any further in this tutorial.

Update Package

Now lets see how we update our packages as we all need to update things!. We can do this using the -U flag.

python -m pip install <package name> -U

Or we can use --upgrade instead of -U.

python -m pip install <package name> --upgrade

Remove Package

We can also remove packages that are installed.

python -m pip uninstall <package name>

We can also use the -r flag to specify a requirements file like we can do when installing packages.

List Packages

There are several ways we can list our packages we have installed.

List

The first method is to use a human readable format.

When this is run we will get table view output with the package name and the version

python -m pip list

As an example here is what I get as an output:

Package              Version
-------------------- ------------------
aiofiles             0.7.0
aiosqlite            0.16.1

Freeze

Another way is to output in a format we can copy directly into a requirements.txt file. This is very useful when you have finished a project and want to keep a record of the exact packages you have used and what version they are.

python -m pip freeze

As an example here is what I get as an output:

aiofiles==0.7.0
aiosqlite==0.16.1

In GNU/Linux we can pipe the output of the command into a file.

python -m pip freeze >> requirements.txt

Conclusion

As you can see pip is a powerful tool that allows us to manage pip packages. It has much more functionality that is not covered in this tutorial, you can find these at the official docs.

You may want to find out about “venv” which is another python command that allows the creation of Python virtual environments which can help separate installed packages for different projects. I have made a separate tutorial for this which can be found here.

“venv” is only available on Python 3.3 and up

In this tutorial I did not show how we can install specific versions of packages, however you can find out about this in the pip documentation here.