Python - PIP (Package Manager)

Overview

Estimated time: 15–25 minutes

Install and manage packages with pip, using virtual environments for isolation.

Learning Objectives

  • Install, upgrade, and remove packages with pip.
  • Freeze and sync dependencies (requirements.txt).
  • Understand pipx for installing CLI tools globally.

Prerequisites

Basic commands

# inside an activated virtual environment
pip install requests
pip show requests
pip list --outdated
pip install --upgrade requests
pip uninstall requests -y

Pinning and syncing

pip freeze > requirements.txt
pip install -r requirements.txt --no-deps

Tools: pipx for CLI apps

# install pipx once
python -m pip install --user pipx
pipx ensurepath

# install a CLI
pipx install black

Common Pitfalls

  • Installing into the system Python; always use a virtual environment per project.
  • Not pinning versions for applications; use a lock or pinned requirements for reproducibility.

Checks for Understanding

  1. How do you list outdated packages?
  2. Why use pipx?
Show answers
  1. pip list --outdated
  2. To install and isolate CLI tools globally without affecting project envs.

Exercises

  1. Create a new venv, install requests, and write a script that fetches a URL.
  2. Freeze dependencies and recreate the environment from requirements.txt.