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
- How do you list outdated packages?
- Why use
pipx
?
Show answers
pip list --outdated
- To install and isolate CLI tools globally without affecting project envs.
Exercises
- Create a new venv, install
requests
, and write a script that fetches a URL. - Freeze dependencies and recreate the environment from
requirements.txt
.