Installing Packages with pip
Use code the community has shared
In this lesson
pip is Python’s package installer — the tool you use to download and add free, community-built packages from the Python Package Index (PyPI). Beyond Python’s standard library is a vast world of these packages, and knowing how to add, track, and manage them unlocks the wider Python ecosystem.
Explain it like I’m 5
pip is an app store for Python code. You ask for a package by name, and pip downloads it and sets it up so you can import it in your own program.
pip and PyPI
pip is Python’s package installer; PyPI (the Python Package Index) is the online catalogue it downloads from. pip install requests fetches the popular requests library and makes it importable. Always install into an activated virtual environment (previous lesson) so packages stay project-specific.
Installing, listing, and removing
pip install name adds a package; pip install name==2.1.0 installs a specific version. pip list shows what’s installed, pip show name gives details, and pip uninstall name removes it. Prefer python -m pip ... — it guarantees you’re using the pip that belongs to the active Python.
# Run inside an activated virtual environment.
python -m pip install requests==2.32.3
python -m pip list
python -m pip freeze > requirements.txt
Successfully installed requests-2.32.3 # requirements.txt now lists requests and its dependencies
Pinning versions and requirements.txt
To make your project reproducible, record its dependencies. pip freeze > requirements.txt writes every installed package and its exact version to a file. Anyone (including future you) can recreate the environment with pip install -r requirements.txt. Pinning versions means your code won’t silently break when a package releases an update.
# Someone clones your project. Inside their fresh venv:
python -m pip install -r requirements.txt
Successfully installed requests-2.32.3 (and its dependencies)
Adding dependencies safely
Each package is code written by someone else, so add them thoughtfully: prefer well-known, actively maintained libraries; check the project’s page and popularity; and watch for typo-squatting (lookalike names). Fewer, trusted dependencies are easier to maintain and safer than a long list of obscure ones.
Upgrading, inspecting, and where packages live
A few more commands round out everyday pip use. pip install --upgrade name updates a package to its newest version. pip show name prints its version, a one-line summary, its dependencies, and — usefully — exactly where it’s installed.
That location is your active environment’s site-packages folder, which is precisely why activating the right virtual environment matters before you install or import anything.
python -m pip install --upgrade requests
python -m pip show requests
Name: requests Version: 2.32.3 Summary: Python HTTP for Humans. Location: /path/to/project/.venv/lib/python3.13/site-packages Requires: certifi, charset-normalizer, idna, urllib3
Common mistake: Installing globally instead of into a venv
Forgetting to activate a virtual environment means packages land in the global Python and pile up across unrelated projects.
Activate the project’s venv first (look for the (.venv) prompt), then install. Keep each project’s dependencies separate.
Common mistake: Not pinning versions, then 'it worked yesterday'
Installing without a version gets whatever is latest, which can change between machines or over time and break your code.
Record exact versions with pip freeze > requirements.txt and install from it, so everyone gets the same versions.
What is pip?
pip installs third-party packages from the Python Package Index (PyPI).
What does pip freeze > requirements.txt accomplish?
It captures the exact dependency versions for reproducible installs elsewhere.
Mini exercise (medium)
Write the commands to install version 2.31.0 of requests into an active venv, then save the project’s dependencies to a file others can install from.
pip runs in a terminal, but you can build the pieces here. Set the pinned install command and the freeze command as strings.
install = "..." # install requests, pinned to 2.31.0
freeze = "..." # save dependencies to requirements.txt
print(install)
print(freeze)
Pin with ==2.31.0, then use pip freeze redirected to requirements.txt.
install = "pip install requests==2.31.0"
freeze = "pip freeze > requirements.txt"
print(install)
print(freeze)
assert install == "pip install requests==2.31.0", "install with: pip install requests==2.31.0"
assert freeze == "pip freeze > requirements.txt", "save with pip freeze into requirements.txt"
print("✓ Commands correct!")