#19: Working with Packages in Python
The ecosystem of Python with all the helpful third-party libraries makes it such a productive language. It is therefore time to look where we can find them and how we can install those packages. For this post I use the term packages to describe the distribution units of Python code, not the way how you can organise your code using folders and the __init__.py file. That will be the topic of a later post.
PyPI – the Python Package Index
Until now all the things I explained in the Python Friday series used the built-in packages. Like all other programming languages, Python has already a lot of things built-in that you can use to create your application. However, we do not need to reinvent the wheel every time we have to solve a problem. Often there is already a solution that we can reuse. For Python you find the packages on PyPI.org.

At the time of writing this post you can find more than 224’000 projects on PyPI. While the search function is a great help, you may still not find what you are looking for. A good help to find packages for a specific use case is to check the curated list of Awesome Python. If you want to do something with email, you click on the email entry and only need to check the 6 libraries it suggests:

Pip – the Python Package Installer
You can use the tool pip for all things related to packages. You can install packages, remove them or get the newest version. Before we can do anything, we need to install pip itself. If you have a current version of Python installed on your system, then you most likely have pip as well. Run this command in the Command Prompt (cmd) to check if this the case:
If this command results in an error, then go to http://pip-installer.org/ and follow the instructions to install pip.
WARNING
The commands in the rest of this post will modify your system. If you know what you do, then everything is OK. However, in most cases you want to install packages for a specific project and not for your whole system. The next post will be about virtual environments that solve exactly this problem.
Getting a list of installed packages
To see all installed packages on your environment, you can run pip list:
Install a package
To install the newest version of a package, you can use the install command:
The install command not only installs the package you specify, but also all its dependencies.
Sometimes you need a specific version of a package. You can submit the version you need directly after the package name:
Remove a package
You can remove an installed package using the uninstall command:
The uninstall command does only uninstall the package you specify, not its dependencies. If you want to clean-up you need to do this on your own.
Upgrade a package
You can upgrade a package using the –upgrade option for the install command. Before you run this command you should read the documentation and check if the upgrade strategy of pip is what you need.
You can use the same command to upgrade pip itself. When a new version of pip is available, you will get a warning like this one at the end of the output of any pip command:
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
List outdated packages
Finding outdated packages is in many programming languages a challenge. In Python you can simply use pip list -o to get this list:
Get information about installed packages
You can use the show option to get the basic information about a package. Next to the version number you most likely want to know the web site or the location of this package on your machine:
Next
There is an incredible number of packages ready to use with your applications. You can find them in the PyPI and install them with pip. However, before you know it your system is filled with packages you no longer need, and everything looks like a mess. The next post will show you a way to prevent your system to end up as a package dump.