Course Progress66%
🍎 Python Practical Python Topic 66 / 100
⏳ 7 min read

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."

— ShurAI

What 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:

Essential pip commands — run in your terminal:
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
pip3 vs pip

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:

terminal — run this once
pip install requests
python — now use it
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:

terminal — create requirements.txt from your environment
pip freeze > requirements.txt
requirements.txt — example file
requests==2.31.0
pandas==2.1.0
numpy==1.26.0
flask==3.0.0
terminal — install everything from requirements.txt
pip install -r requirements.txt

Popular Packages to Know

requests
HTTP requests — talk to any API or website
pandas
Data analysis with DataFrames — like Excel in Python
numpy
Fast numerical computing and arrays
flask
Build web apps and APIs with minimal code
pillow
Image processing — resize, crop, filter photos
rich
Beautiful terminal output with colours and tables

Real Example — Fetching a Joke from an API

terminal — install first
pip install requests
python
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?