Skip to main content

Chapter 1: Getting started with Python Language

📦 Section 1.13: Managing External Packages with pip

Introduction to pip and Package Management

pip is Python's official package installer and is used to install packages from the Python Package Index (PyPI). It helps manage libraries and dependencies that are not included in the standard Python library, making it easier to enhance your projects with a wide range of functionalities available from the community.

Key Features of pip:

  • Installing packages: Download and install packages from PyPI or other indexes.
  • Removing packages: Uninstall packages that are no longer needed.
  • Upgrading packages: Update packages to their latest versions.
  • Listing packages: Show all installed packages and their versions.
  • Managing requirements: Install all the packages listed in a requirements file.

Why Use pip?

  • Extensive library access: Gain access to over 200,000 projects on PyPI.
  • Easy to use: Simple commands to manage all package needs.
  • Compatibility: Works well with virtual environments to manage dependencies for different projects.

Installing and Updating Packages with pip

Installing a Package

  • To install a package using pip, you use the install command followed by the name of the package. This command downloads and installs the package and its dependencies.
    • pip install requests: This command will download and install the requests library, which is widely used for making HTTP requests in Python.

Example:

# Installing the requests library
pip install requests

Updating a Package

  • If a package is already installed, you can update it to the latest version available on PyPI using the install command with the --upgrade flag.
    • pip install --upgrade requests: This command updates the requests library to the latest version, ensuring you have access to the newest features and bug fixes.

Example:

# Updating the requests library
pip install --upgrade requests

Using Requirements Files

  • For managing multiple dependencies, you can use a requirements file (typically named requirements.txt). This file contains a list of items to be installed using pip install.
    • pip install -r requirements.txt: This command reads the requirements.txt file and installs all the specified packages along with their correct versions.

To install all the dependencies listed in the requirements.txt file:

# Installing packages from a requirements file
pip install -r requirements.txt

Example of a requirements.txt file:

requests==2.25.1
numpy>=1.18.5
pandas

Listing Installed Packages

  • You can list all installed packages and their versions using the list command.
    • pip list: This command displays a list of all packages installed in the current environment, helping you keep track of your project's dependencies.

Example:

# Listing installed packages
pip list

Conclusion

pip is an essential tool for Python developers, providing an efficient way to manage external packages and dependencies. Understanding how to use pip to install, update, and manage packages can greatly streamline the development process and ensure that projects are both efficient and up-to-date.