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
— step through the idea, then dive into the details below.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.
#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.
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.
# 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.0pip3 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:
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.
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:
# 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:
# 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.1Now your friend (or a deployment server) can install every dependency in one command:
# Terminal command to install from requirements.txt:
# pip install -r requirements.txt
# pip reads every line and installs the listed packages.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 environmentsource .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.
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.
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).
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.
A script imports `requests` and checks a response. What does this code print?
import requests
response = requests.get("https://httpbin.org/get")
print(response.status_code)
print(type(response))This code tries to parse the JSON body from an API response, but it crashes. What is wrong?
import requests
response = requests.get("https://httpbin.org/json")
data = response.json
print(data)Complete the terminal command that saves all currently installed packages and their exact versions to `requirements.txt`.
pip > requirements.txt
Put these lines in the right order to fetch a URL, parse the JSON response, and print a value from it.
data = response.json()
print(data["uuid"])
import requests
response = requests.get("https://httpbin.org/uuid")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: