pip and Packages
Install any of 500,000+ libraries from PyPI with one command — and manage your project’s dependencies cleanly.
"Python's superpower is its ecosystem. There are over 500,000 packages on PyPI. Whatever you want to build, someone has probably already built a foundation for it."
— ShurAIWhat is pip?
pip is Python’s package installer. It downloads and installs libraries from PyPI (the Python Package Index — pypi.org), which hosts over 500,000 free packages. You run pip commands in your terminal, not in Python:
pip install requests
Install a package
pip install requests==2.31.0
Install a specific version
pip uninstall requests
Remove a package
pip list
Show all installed packages
pip show requests
Show details about one package
pip install --upgrade requests
Upgrade to the latest version
On some systems (especially Linux/Mac) you may need to type pip3 instead of pip to ensure you’re using Python 3. Inside a virtual environment (next lesson), plain pip always works correctly.
Installing and Using a Package
Install requests once in your terminal, then import and use it in Python:
pip install requests
import requests
response = requests.get("https://api.github.com")
print(response.status_code) # 200
print(response.json()) # dict of GitHub API info
requirements.txt — Sharing Dependencies
When you share a project with others, list all your packages in requirements.txt so they can install everything with one command:
pip freeze > requirements.txt
requests==2.31.0
pandas==2.1.0
numpy==1.26.0
flask==3.0.0
pip install -r requirements.txt
Popular Packages to Know
Real Example — Fetching a Joke from an API
pip install requests
import requests
def get_joke():
"""Fetch a programming joke from the JokeAPI."""
url = "https://v2.jokeapi.dev/joke/Programming?type=single"
resp = requests.get(url)
if resp.status_code == 200:
data = resp.json()
return data.get("joke", "No joke found")
else:
return f"Error: {resp.status_code}"
print(get_joke())
# A SQL query walks into a bar, walks up to two tables and asks...
# "Can I join you?"
"Never reinvent the wheel. Before writing something from scratch, check PyPI. There's almost certainly a well-tested, well-documented package that does exactly what you need."
— ShurAI🧠 Quiz — Q1
What is PyPI?
🧠 Quiz — Q2
How do you install a specific version of a package?
🧠 Quiz — Q3
What does pip freeze > requirements.txt do?
🧠 Quiz — Q4
How do you install all packages listed in a requirements.txt file?