pip: Manage Third-Party Packages Effectively

Introduction

In this chapter, you will learn pip, Python's standard package manager. With pip, you can install, upgrade, remove, and manage third-party libraries for your projects. Mastering pip is essential for real-world Python development because almost every practical project depends on external packages.

Prerequisites

  • Python 3.10+ installed
  • Basic command-line usage
  • Basic understanding of Python modules and packages

What Is pip

pip is the default tool for managing Python packages from package indexes (such as PyPI).

Common tasks:

  • install new libraries
  • upgrade or uninstall packages
  • freeze dependency lists
  • reproduce environments

Check pip version:

bash
pip --version

Or with Python explicitly:

bash
python -m pip --version

1) Install Packages

Install a package:

bash
pip install requests

Install a specific version:

bash
pip install requests==2.32.3

Upgrade package:

bash
pip install --upgrade requests

Tip

Best Practice

Prefer python -m pip ... in scripts/docs to ensure you use the pip bound to the intended Python interpreter.

2) Uninstall and List Packages

Uninstall:

bash
pip uninstall requests

List installed packages:

bash
pip list

Show details of one package:

bash
pip show requests

3) Dependency Snapshot with requirements.txt

Export current environment packages:

bash
pip freeze > requirements.txt

Install from dependency file:

bash
pip install -r requirements.txt

This is key for reproducible projects and team collaboration.

4) Virtual Environment + pip Workflow

In real projects, use virtual environments to avoid dependency conflicts.

Create virtual environment:

bash
python -m venv .venv

Activate (Windows PowerShell):

powershell
.\.venv\Scripts\Activate.ps1

Activate (macOS/Linux):

bash
source .venv/bin/activate

Then install packages inside virtual environment:

bash
python -m pip install requests

5) Real Mini Example: Install and Use requests

Step 1: install package

bash
python -m pip install requests

Step 2: create script

python
import requests
 
url = "https://api.github.com"
response = requests.get(url, timeout=10)
 
print("Status code:", response.status_code)
print("Content type:", response.headers.get("Content-Type"))

This is a simple but real-world package usage flow.

6) Common pip Commands Cheat Sheet

  • pip install <pkg>: install package
  • pip install -U <pkg>: upgrade package
  • pip uninstall <pkg>: remove package
  • pip list: list installed packages
  • pip show <pkg>: package metadata
  • pip freeze: output pinned dependency versions
  • pip install -r requirements.txt: install from file

Warning

Avoid installing packages globally for all projects.
Use virtual environments to prevent version conflicts.

Common Beginner Mistakes

Mistake 1: Installing to Wrong Python Environment

When multiple Python versions exist, pip may point to a different interpreter.

Mistake 2: Forgetting requirements.txt

Without dependency snapshots, teammates cannot reproduce your environment reliably.

Mistake 3: Mixing Global and Project Dependencies

This often leads to hard-to-debug conflicts and "works on my machine" issues.

Surprise Practice Challenge

Build a "Dependency Practice Project":

  1. Create virtual environment
  2. Install requests and python-dateutil
  3. Write a small script that imports and uses both packages
  4. Export requirements.txt
  5. Rebuild project in a fresh folder using only requirements.txt

If you finish this, you already have practical dependency-management skills.

FAQ

Why use python -m pip instead of just pip?

It ensures the pip command matches the Python interpreter you intend to use.

Should I commit requirements.txt?

Yes, for application projects and reproducibility.

Can pip install from a local file?

Yes. Example: pip install ./some_package.whl.

Is pip enough for all environment management needs?

For many projects yes, but tools like uv, poetry, or conda may offer additional workflows.