LearnContact
Lesson 3125 min read

Package Management (pip)

Learn how to install, inspect, upgrade, and remove Python packages with pip, and how to record and recreate dependencies using requirements.txt.

Introduction

Python's standard library is large, but the real power of the language comes from its enormous ecosystem of third-party packages — for web development, data science, automation, and almost anything else you can imagine. pip is the tool that installs, manages, and removes these packages.

In this lesson, you will learn how to install packages with pip, install a specific version, inspect what is installed, remove packages you no longer need, and share your project's exact dependencies with others using a requirements.txt file.

What You Will Learn
  • What pip is and how it fits into the Python ecosystem.
  • How to install, list, inspect, and uninstall packages.
  • How to install a specific package version.
  • How to create and use a requirements.txt file.
  • What PyPI is, and how to keep pip itself up to date.

What is pip?

pip stands for "Pip Installs Packages" (a recursive acronym). It is Python's official package manager, bundled with modern Python installations, and it is the standard tool for downloading and installing third-party libraries from the internet.

Package Installer

pip downloads and installs Python packages so you can import and use them.

Connects to PyPI

By default, pip fetches packages from the Python Package Index (PyPI).

Comes Pre-Installed

Modern Python installations already include pip — no separate setup needed.

Works Inside a venv

Always activate your virtual environment first so installs stay isolated to that project.

Activate Your venv First

Before running any pip command for a project, make sure your virtual environment (from the previous lesson) is activated, so packages install into that isolated environment rather than globally.

Installing Packages

The basic command to install a package is pip install followed by the package's name.

Install a Package
pip install requests
Output
Collecting requests
  Downloading requests-2.31.0-py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.31.0

Once installed, the package can be imported and used in your code right away.

Using an Installed Package
import requests

response = requests.get("https://api.github.com")
print(response.status_code)
Output
200

You can also install several packages at once by listing them separated by spaces.

Install Multiple Packages
pip install requests flask pandas

Installing a Specific Version

Sometimes a project depends on an exact version of a package, for compatibility reasons. You can request one using == after the package name.

Install an Exact Version
pip install requests==2.28.0

You can also specify a minimum or maximum version using comparison operators.

Install a Version Range
pip install "requests>=2.25,<3.0"
Why Pin Versions?

Pinning versions ensures that everyone working on the project — and any server that deploys it — uses the exact same package version, avoiding "it works on my machine" bugs caused by version differences.

Listing & Inspecting Packages

pip list shows every package currently installed in the active environment, along with its version.

List Installed Packages
pip list
Output
Package         Version
--------------- -------
pip             24.0
requests         2.31.0
urllib3          2.2.1

To see detailed information about one specific package — including its version, location, and dependencies — use pip show.

Show Package Details
pip show requests
Output
Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Location: /home/user/my_project/venv/lib/python3.12/site-packages
Requires: certifi, charset-normalizer, idna, urllib3
Required-by: 

Uninstalling Packages

If a package is no longer needed, remove it with pip uninstall. pip will ask for confirmation before removing it.

Uninstall a Package
pip uninstall requests
Output
Found existing installation: requests 2.31.0
Uninstalling requests-2.31.0:
  Would remove:
    ...
Proceed (Y/n)? y
  Successfully uninstalled requests-2.31.0

requirements.txt

A requirements.txt file lists every package (and usually its exact version) that a project depends on. It lets anyone — a teammate, a server, or future you — recreate the exact same environment with one command.

You can generate this file automatically from your currently active environment using pip freeze, redirected into a file.

Generate requirements.txt
pip freeze > requirements.txt
Example requirements.txt
certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.6
requests==2.31.0
urllib3==2.2.1

Anyone who receives your project can then recreate the exact same set of packages in their own (activated) virtual environment with a single command.

Install from requirements.txt
pip install -r requirements.txt
Standard Workflow
  • Create and activate a virtual environment.
  • Install the packages your project needs with pip install.
  • Run pip freeze > requirements.txt to record them.
  • Commit requirements.txt to version control (not the venv folder itself).
  • Others run pip install -r requirements.txt to match your environment exactly.

What is PyPI?

PyPI (the Python Package Index, pronounced "pie-pee-eye") is the official, public repository that hosts hundreds of thousands of Python packages. When you run pip install package_name, pip contacts PyPI by default, downloads the package, and installs it into your environment.

pypi.org

The website where you can search and browse available Python packages.

Publishing

Developers can publish their own packages to PyPI for anyone to install.

Discovery

You can check a package's description, version history, and documentation before installing.

Default Source

pip is pre-configured to use PyPI unless you specify a different package index.

Upgrading pip

pip itself is a package that gets updated over time, and it is good practice to keep it current. You can upgrade pip using pip itself.

Upgrade pip
python -m pip install --upgrade pip
Output
Successfully installed pip-24.1.2
Why python -m pip Instead of Just pip?

Running pip through python -m pip ensures you are using the pip that belongs to the specific Python interpreter currently active (for example, the one inside your activated virtual environment), avoiding confusion when multiple Python versions are installed.

Common Mistakes

Avoid These Mistakes
  • Running pip install without activating the project's virtual environment first.
  • Forgetting to update requirements.txt after installing a new package.
  • Committing the venv folder to version control instead of just requirements.txt.
  • Assuming pip freeze only shows packages you explicitly installed — it lists every installed package, including sub-dependencies.
  • Ignoring version pins in requirements.txt and installing whatever the latest version happens to be.

Best Practices

  • Always activate your virtual environment before running any pip command.
  • Regenerate requirements.txt with pip freeze after installing or removing packages.
  • Pin exact versions in requirements.txt for consistent, reproducible environments.
  • Periodically upgrade pip itself with python -m pip install --upgrade pip.
  • Check a package's page on pypi.org before installing something unfamiliar.

Frequently Asked Questions

Do I need to install pip separately?

No. Modern versions of Python (3.4+) come with pip already installed.

What is the difference between pip list and pip freeze?

pip list shows a human-readable table of installed packages and versions. pip freeze outputs the same information in the exact package==version format needed for a requirements.txt file.

Can I install packages from somewhere other than PyPI?

Yes, pip supports installing from other package indexes, version control URLs, or local files, but PyPI is the default and by far the most common source.

What happens if I run pip install -r requirements.txt without a virtual environment active?

The packages will be installed into whatever Python environment is currently active — which could be your global Python installation. Always activate your venv first.

Should requirements.txt be committed to version control?

Yes. Unlike the venv folder itself, requirements.txt is small, text-based, and exactly what teammates or servers need to recreate your dependencies.

Key Takeaways

  • pip is Python's official package manager, used to install and remove third-party packages.
  • pip install package installs the latest version; pip install package==x.y.z installs a specific one.
  • pip list shows installed packages; pip show package shows details about one specific package.
  • pip freeze > requirements.txt records exact dependencies; pip install -r requirements.txt recreates them.
  • PyPI is the default public package index pip downloads from, and pip itself should be kept up to date.

Summary

pip is the tool that connects your project to Python's vast ecosystem of third-party packages hosted on PyPI. Installing, inspecting, and removing packages are all single commands, and requirements.txt lets you record and reproduce a project's exact dependencies anywhere.

Combined with the virtual environments from the previous lesson, pip gives you full, isolated, reproducible control over exactly what each of your Python projects depends on.

Lesson 31 Completed
  • You can install, inspect, and uninstall packages with pip.
  • You can install an exact package version when needed.
  • You can generate and use a requirements.txt file.
  • You understand what PyPI is and how to keep pip itself updated.
Next Lesson →

File Handling