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:
pip --versionOr with Python explicitly:
python -m pip --version1) Install Packages
Install a package:
pip install requestsInstall a specific version:
pip install requests==2.32.3Upgrade package:
pip install --upgrade requestsTip
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:
pip uninstall requestsList installed packages:
pip listShow details of one package:
pip show requests3) Dependency Snapshot with requirements.txt
Export current environment packages:
pip freeze > requirements.txtInstall from dependency file:
pip install -r requirements.txtThis 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:
python -m venv .venvActivate (Windows PowerShell):
.\.venv\Scripts\Activate.ps1Activate (macOS/Linux):
source .venv/bin/activateThen install packages inside virtual environment:
python -m pip install requests5) Real Mini Example: Install and Use requests
Step 1: install package
python -m pip install requestsStep 2: create script
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 packagepip install -U <pkg>: upgrade packagepip uninstall <pkg>: remove packagepip list: list installed packagespip show <pkg>: package metadatapip freeze: output pinned dependency versionspip 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":
- Create virtual environment
- Install
requestsandpython-dateutil - Write a small script that imports and uses both packages
- Export
requirements.txt - 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.