Installing Packages with pip

Use code the community has shared

Beginner 10 min

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.

Example
# Run inside an activated virtual environment.
python -m pip install requests==2.32.3
python -m pip list
python -m pip freeze > requirements.txt
Output
Successfully installed requests-2.32.3
# requirements.txt now lists requests and its dependencies
Install a pinned version and record it. (Run locally.)

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.

Example
# Someone clones your project. Inside their fresh venv:
python -m pip install -r requirements.txt
Output
Successfully installed requests-2.32.3 (and its dependencies)
Recreate the exact environment from the file. (Run locally.)

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.

Example
python -m pip install --upgrade requests
python -m pip show requests
Output
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
Upgrade a package, then inspect it. (Run locally.)

Common mistake: Installing globally instead of into a venv

Why it happens:

Forgetting to activate a virtual environment means packages land in the global Python and pile up across unrelated projects.

How to fix it:

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'

Why it happens:

Installing without a version gets whatever is latest, which can change between machines or over time and break your code.

How to fix it:

Record exact versions with pip freeze > requirements.txt and install from it, so everyone gets the same versions.

What is pip?

What does pip freeze > requirements.txt accomplish?

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)

What to learn next

You learned what pip and PyPI are, how to install and uninstall packages, how to pin versions, why requirements.txt makes a project reproducible, and how to stay safe when adding dependencies. You built a pinned install command and the freeze command that captures your dependencies.

Now that you can build real scripts, it’s useful to let people configure them at launch. Command-Line Arguments shows how to read sys.argv, guard against missing values, convert argument strings, and take a first look at argparse. When you’re ready, continue to the next lesson.