9.3 Package Management with pip

9.3 Package Management with pip

Introduction to the pip package manager for installing and managing external libraries from the Python Package Index (PyPI).

While Python's Standard Library is extremely rich, the real power of Python comes from the vast ecosystem of third-party packages. The **PyPI (Python Package Index)** is the official software repository for Python, hosting hundreds of thousands of packages. **pip** is the de facto command-line tool we use to install and manage these packages.
The following commands are executed in your computer's terminal (or Command Prompt), not in the live editor.

Basic pip Commands

# Install a package. pip will find it on PyPI and download it.
# pip install requests

# Install a specific version
# pip install requests==2.25.0

# Upgrade a package to its latest version
# pip install --upgrade requests

# Uninstall a package
# pip uninstall requests

# Display a list of all installed packages and their versions
# pip list

The requirements.txt File: The Heart of Dependencies

It is extremely important for every project to have a reproducible way to install all its dependencies. The best practice is to list all the packages your project needs in a simple text file, usually named `requirements.txt`.
# "Freezes" the current state of packages and saves it to the file.
# pip freeze > requirements.txt

# Install all packages listed in the file.
# This allows another developer to set up your project with a single command.
# pip install -r requirements.txt

Explore More with AI

Use AI to generate new examples, delve deeper into theory, or get your questions answered.