Modules & PackagesBeginner7 min47 / 63

pip & External Packages

Learn how to install third-party packages from PyPI using pip, import them in your code, and track your project's dependencies with a requirements.txt file.

Python ships with a great standard library, but the real superpower is the enormous ecosystem of third-party packages built by developers around the world. Need to make web requests? Send emails? Analyze data? Chances are someone has already written a package for it — and you can add it to your project in seconds.

See it in action

Visual walkthrough1 / 5
1

Python's Giant App Store

Python comes with a solid standard library, but the real magic is PyPI — over 500,000 free packages built by developers worldwide. Need to fetch a web page or crunch data? Someone already wrote it.

PyPI stands for Python Package Index and lives at pypi.org.

#What is PyPI?

PyPI (the Python Package Index, at pypi.org) is the official online store for Python packages. It hosts over 500,000 packages — all free to download. Think of it like an app store, but for Python code.

Think of it like

Like an App Store

PyPI is the app store. pip is the install button. Your Python project is the phone. You browse (or search) the store, pick what you need, and pip downloads and installs it for you automatically.

#Installing a Package with pip

pip is Python's built-in package installer. You run it from your terminal (not inside a Python file). The basic command is:

`` pip install <package-name> ``

Let's install requests, one of the most popular Python packages ever. It makes sending HTTP requests (fetching web pages, calling APIs) very simple.

Running pip install in your terminal
# Run this in your terminal, NOT in a Python file:
# pip install requests

# You will see output like:
# Collecting requests
#   Downloading requests-2.31.0-py3-none-any.whl
# Installing collected packages: requests
# Successfully installed requests-2.31.0
Tip

pip3 vs pip

On some systems you may need to type pip3 instead of pip to make sure you are installing for Python 3. If pip gives you an error, try pip3 install requests.

#Using an Installed Package

Once a package is installed, you use import just like any other module. Here is a small example using requests to fetch data from a public API:

Fetching a URL and checking the response status code
import requests

response = requests.get("https://httpbin.org/get")
print(response.status_code)
print(type(response))

A status code of 200 means the request was successful. The requests library handles all the complicated networking for you — you just call requests.get() with a URL and you're done.

Parsing a JSON response into a Python dictionary
import requests

response = requests.get("https://httpbin.org/json")
data = response.json()   # parse the JSON body into a Python dict
print(data)

#Seeing What's Installed: pip list

Want to see every package currently installed? Run pip list in your terminal. You will get a neat table:

pip list shows all installed packages and their versions
# Terminal command:
# pip list

# Example output:
# Package    Version
# ---------- -------
# certifi    2024.2.2
# charset-normalizer 3.3.2
# idna       3.6
# pip        24.0
# requests   2.31.0
# urllib3    2.2.1

#Saving Dependencies: requirements.txt

Imagine you share your project with a friend. They clone your code, but requests is not installed on their machine — your program crashes immediately. The fix is a requirements.txt file that lists every package your project needs.

You can generate one automatically with pip freeze:

pip freeze captures exact package versions into a file
# Terminal command to create requirements.txt:
# pip freeze > requirements.txt

# The file will contain lines like:
# certifi==2024.2.2
# charset-normalizer==3.3.2
# idna==3.6
# requests==2.31.0
# urllib3==2.2.1

Now your friend (or a deployment server) can install every dependency in one command:

Restoring all dependencies from requirements.txt
# Terminal command to install from requirements.txt:
# pip install -r requirements.txt

# pip reads every line and installs the listed packages.
Note

Commit requirements.txt to version control

Always include requirements.txt in your Git repository. It is the contract that tells anyone running your code exactly which packages they need.

#A Word on Virtual Environments

When you run pip install without any extra setup, packages are installed globally — meaning every Python project on your machine shares them. This can cause problems: Project A needs requests version 2.28, but Project B needs version 2.31.

The solution is a virtual environment — a self-contained folder that holds its own separate copy of Python and packages, just for one project. You create one with:

  • python -m venv .venv — creates the environment
  • source .venv/bin/activate (Mac/Linux) or .venv\Scripts\activate (Windows) — activates it

After activating, pip install only affects that project. This is the professional way to manage packages and is strongly recommended.

Common mistake

Installing without a virtual environment can cause conflicts

If two projects need different versions of the same package and you install globally, one of them will break. Always use a virtual environment for any real project. It takes 30 seconds to set up and saves hours of headaches later.

Watch out

Only install packages you trust

PyPI is open — anyone can publish a package. Before installing something, check it has a good number of downloads, recent maintenance, and a reputable source. Typos in package names can lead to installing malicious code (a trick called typosquatting).

Quick check

You want to save your project's current dependencies so a teammate can install them easily. Which command creates the requirements.txt file?

Key takeaways

  • PyPI is the central repository of third-party Python packages — over 500,000 are available for free.
  • Use `pip install <package>` in your terminal to download and install a package.
  • After installing, import the package in your Python file just like any other module.
  • Use `pip freeze > requirements.txt` to record dependencies, and `pip install -r requirements.txt` to restore them.
  • Always use a virtual environment so different projects do not interfere with each other's packages.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

A script imports `requests` and checks a response. What does this code print?

predict-output
import requests

response = requests.get("https://httpbin.org/get")
print(response.status_code)
print(type(response))
Fix the bug#2

This code tries to parse the JSON body from an API response, but it crashes. What is wrong?

fix-bug
import requests

response = requests.get("https://httpbin.org/json")
data = response.json
print(data)
Fill in the blank#3

Complete the terminal command that saves all currently installed packages and their exact versions to `requirements.txt`.

pip  > requirements.txt
Reorder the lines#4

Put these lines in the right order to fetch a URL, parse the JSON response, and print a value from it.

1
data = response.json()
2
print(data["uuid"])
3
import requests
4
response = requests.get("https://httpbin.org/uuid")
Your turn
Practice exercise

Install the requests package (if you haven't already), then write a Python script that fetches the URL https://httpbin.org/uuid and prints the uuid value from the JSON response. The response looks like {"uuid": "some-unique-id"}. Your script should print just the UUID string.

Try it live — edit the code and hit Run to execute real Python:

solution.py · editable