"
This article is part of in the series
Published: Thursday 31st March 2022
Last Updated: Wednesday 14th December 2022

Python packages comprise large chunks of code that you can repeatedly use in different programs. They remove the need for writing code that’s already been written, hence it comes in really handy for programmers and other professions where Python is widely used such as machine learning engineers and data analysts. You can import packages such as matplotlib and numpy into a project. Each package comes with several functions that you can use in your code. However, before you can use a package, you will need to install it using pip, which is Python’s default package manager. You also need to understand how to update packages and uninstall packages that you don’t need. This brief guide covers everything you need to know. 

Operations of Pip Explained

Installing pip

If you install Python on your computer using the installer on python.org, pip is installed along with Python. Pip is also automatically installed if you work in a virtual environment or use a Python version that isn’t modified by a redistributor. Redistributors typically remove the ensurepip module from the Python install. If you are using a modified version of Python, you can install pip using get-pip.py or ensurepip. This will work on both Windows and macOS machines. However, setting up a fresh Python environment with the python.org installer is often easier. 

Using Different Versions of pip 

If you have installed Python2 and Python3 on your computer, you should be able to use pip2 and pip3 in addition to using the pip command. Pip can be set up to work on either Python2 or Python3 on one machine. It will not work with both. For instance, if you set up pip to work with Python3, the packages you install with it will not work on Python2. pip2 can be used to manage packages in Python2, and pip3 can be used for package management in Python3.

Checked the Details of an Installed Package

If you’re unsure whether you have pip installed or not, you can use the pip show command to find the details of any installed package. The syntax of pip show is:

pip show <package-name>

Running the code to find the license and dependencies of pip should give a similar result as below if pip is installed on the machine:

pip show pip
Name: pip
Version: 18.1
Summary: The PyPA recommended tool for installing Python packages.
Home-page: [https://pip.pypa.io/](https://pip.pypa.io/)
Author: The pip developers
Author-email: [email protected]
License: MIT
Location: /usr/local/lib/python2.7/site-packages
Requires:
Required-by:

Bear in mind that the code above shows that the pip command installs packages in …/python2.7/site-. However, depending on the environment, pip can also be used on Python3.

Listing All Installed Packages

Pip also equips you with the ability to generate a list of the packages installed for Python on your computer.  Entering “pip list” on the interpreter should give you a similar output:

pip list
Package    Version
---------- -------
future     0.16.0
pip        18.1
setuptools 39.2.0
six        1.11.0
wheel      0.31.1

You can alter the output format of the command to only output up-to-date packages, outdated packages, or packages that do not have dependencies. You can also use the pip freeze command. The command will not output pip and package management packages such as wheel and setup tools.

pip freeze
future==0.16.0
six==1.11.0

Installing a Package Installing a package with pip is easy. If the package you want to install is registered in the PyPI, all you need to do is specify the name of the package, like so:

pip install <package-name>

Running this command will install the latest version of the package. You can also use pip install to install multiple packages in one go.

pip install <package_name_1> <package_name_2> <package_name_3> ...

Pip also allows you to install a specific version of a package using the syntax:

pip install <package-name>==<version>

Installing from GitHub/Local Drive

Sometimes, the new versions of some packages are not updated timely on the PyPI. In such cases, developers install it from a local directory or a GitHub repository. To install a package from a local directory using pip install, specify the path containing the setup.py file, like so:

pip install path/to/dir

You can install packages from .whl and .zip files as long as they contain a setup.py file.

pip install path/to/zipfile.zip

To install a package from the Git repository, you can use the following command:

pip install git+<repository-url>

To specify a tag or branch and install a specific version of a package, put a “@” at the end of the repository’s URL and type in the package’s tag. Bear in mind that to install packages from GitHub with this method, you will need to have git installed on your system. To get around installing git, you can also download the zip file of the package you want to install from GitHub and install it that way.

Updating a Package

You can update packages to their latest versions by using the --upgrade option with pip install:

pip install --upgrade <package-name>
#or
pip install -U <package-name>

Updating Pip

You can update pip using pip without any hassle. When an update for pip is available, and you run a pip command, you will see a message that says, “You are using pip version xy.a, however version xy.b is available.” You can run “pip install --upgrade pip” to install and use the new version of pip. To update pip2 or pip3 using this command, only replace the first pip with the pip version.

pip3 install --upgrade pip
#or
pip2 install --upgrade pip

Uninstalling a Package

Uninstalling packages with pip is just as simple as installing packages with it. You can use the following command to uninstall packages:

pip uninstall <package-name>

Besides enabling you to install multiple packages simultaneously, pip also allows you to uninstall multiple packages in one go. You must use the command:

pip uninstall <package-name1> <package-name2> <package-name3> ...

When you delete packages using pip, you will be asked to confirm whether you want to delete the files. If you don’t want the prompt to appear for the packages you want to uninstall, you can use the --yes or -y option after “pip uninstall.”

Checking for Dependencies

The pip check command allows you to check and verify that installed packages have compatible dependencies.  When you run the command on a machine with all packages having required dependencies, a “no broken requirements found” prompt appears on the screen. On the other hand, if a dependent package is not installed or there is a version mismatch, pip check will output the package and dependency. If you see a message like this, update the corresponding packages with pip install -U.

Learn how to test python applications using pytest, check out the article here