Lesson · Virtual Environments, Dependencies & Packaging
pip & Dependencies
65 min readpip & Dependencies
`pip` installs distributions from package indexes or local sources. Reproducible dependency management requires more than installation: you also need version policy, lock or pin strategy, and controlled upgrades.
Learning goals
- Install a distribution into the active environment with
python -m pip installand confirm it withpython -m pip show. - Explain why
python -m pipbeats a barepip: the-mform binds the tool to the interpreter you launched. - Install from a requirements file and say which lines pin an exact release and which allow upgrades.
- Describe what a package index is, and when the default index must be supplemented.
- Upgrade and uninstall packages with the commands that match each operation, and name one risk behind each.
Mental model
pip only ever changes the environment it runs inside, and every operation (install, upgrade, uninstall) is applied to the installed set that environment already holds. The requirement string is the input, the index is the source, and site-packages plus pip's metadata are what an operation leaves behind.
Mental model
read it top to bottom
requirement string requests, or requests==<version> ↓ pip resolves it from the package index serving distributions download and install into the active environment's site-packages ↓ metadata recorded pip now knows the version and location upgrade or uninstall later commands act on that same record
Core idea
Dependency management is version management: the pip commands are simple, and the decisions are which versions you accept and where those decisions get recorded. Installation pulls from an index into the active environment, --upgrade moves a package forward, and uninstall removes it, but only a written constraint such as a pin (package==x.y.z) or a compatible range makes the set reproducible for the next machine. The planning question is therefore not how to install something, but what you are promising about the result afterwards.
Installing
Installing means adding a distribution and its dependencies to the active environment's site-packages, so its modules become importable in that environment. python -m pip install requests performs that addition, python -m pip show requests reads the recorded metadata back, and the == syntax requests an exact release.
Upgrading
Upgrading replaces an installed distribution with a newer release, normally through python -m pip install --upgrade requests. Upgrades are also where dependency conflicts surface, because a new release may require versions of its own dependencies that another installed package does not allow.
Uninstalling
Uninstalling removes a distribution's files and its recorded metadata with python -m pip uninstall requests. pip does not remove or check reverse dependencies, so uninstalling a package that other installed packages import leaves them broken until the missing distribution is reinstalled.
Package indexes
In pip & Dependencies, Package indexes names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Requirements files
In pip & Dependencies, Requirements files names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Dependency pinning basics
Pinning fixes exact versions for repeatable environments, while looser constraints allow compatible upgrades. Applications and reusable libraries often need different version policies.
Walkthrough
python -m pip install requestsasks the interpreter that runs pip to addrequeststo its own environment; the-mform matters because a barepipon PATH can belong to a different interpreter.python -m pip show requestsreads the metadata recorded during installation (version, location, dependencies), which is the evidence to check after any install or upgrade.python -m pip uninstall requestsremoves the distribution from the same environment, and the commented linerequests==2.x.yshows the pinned form that makes a future install reproducible instead of selecting the newest release.- Predict before running: which environment
pip showreports once a virtual environment is active, and which fields change between the uninstalled and installed states.
Experiments to try
- Install a small package, run
python -m pip showon it, and find the field that proves which environment received the installation. - Install a package that is already present at an older release, once with and once without
--upgrade, and compare what the two commands report. - Create a requirements file with one unpinned line and one
==-pinned line, install with-r, and inspect whatpip listreports for each. - Install one package into the active environment, deactivate it, and try the same import with the machine's default Python to see how environment-scoped the installation was.
Common mistakes
- Running
pipby name after changing shells: the bare name can resolve to another interpreter's pip, so packages land in an environment you were not looking at, whilepython -m pipbinds the tool to the interpreter that is running. - Upgrading without recording constraints: drift widens between machines and no file describes the versions that actually worked.
- Uninstalling a package without checking who imports it: pip removes files and metadata, and any installed package that depends on it breaks at import time.
- Building a requirements file only from memory:
pip freezecaptures the entire environment including unrelated tools, while a curated direct-dependency list plus pins keeps installation lean and reproducible.
Practice: Easy
- Install one small library into the active environment, run
python -m pip show <name>, and name the fields that report its version and location. - Install the same library at an older release with
==, confirm the change withpip show, then restore the newer release with--upgrade. - Uninstall the library and demonstrate the failing import with a single
python -c "import <name>"command.
Practice: Medium
- Write a two-line requirements file (one pinned line, one range such as
>=), install it withpython -m pip install -r requirements.txtinto a fresh environment, and comparepip listbefore and after. - Reproduce a dependency conflict by pinning two packages whose requirements contradict each other; read the resolver error pip prints and identify which requirement it blames.
- Capture
pip freezefrom an existing environment to a file, rebuild the environment in a second folder from that file, and verify both report the same versions.
Practice: Hard
- Design a version policy for a library and an application side by side: say which pins or ranges each should carry and why a reusable library leaves more freedom than an application.
- Build a failure lab: install with the wrong pip (system versus environment), upgrade a pinned dependency under a project, and uninstall a shared dependency; record the mechanism and the recovery step for each case.
- Extend the workflow to a distribution that is not on the default index: point pip at a local wheel or an extra index, install it, and check the metadata afterwards.
Lesson checklist
- I can install, show, upgrade and uninstall a distribution, and say which environment each command touched.
- I can explain what a package index is and where pip looks for distributions.
- I can read a requirements file and say which lines pin and which stay open.
- I can describe what an unrecorded upgrade or a careless uninstall breaks later.
Remember
pip changes exactly one environment at a time: it resolves a requirement, downloads from an index, and records the result in the active `site-packages`. The commands stay easy to memorize, so the real work is the policy they express, which pins you commit to and where those pins live, because that file is what lets the next machine rebuild the same set.
Interview angles
What is the main purpose of **Installing**?+
Installing adds a distribution and its dependencies to the active environment's site-packages. python -m pip install requests performs it, python -m pip show requests reads the recorded metadata back, and the == syntax requests an exact release.
What should you pay attention to when using **Upgrading**?+
python -m pip install --upgrade requests replaces the installed release with a newer one. Upgrades are also where dependency conflicts surface, because a new release may require versions of its dependencies that another installed package does not allow.
How does **Uninstalling** fit into this lesson?+
python -m pip uninstall requests removes the distribution's files and its recorded metadata. pip does not check reverse dependencies, so uninstalling a package that other installed packages import leaves them broken until it is reinstalled.
Why is `python -m pip install ...` preferred over a plain `pip install ...`?+
The -m form runs the pip module of the interpreter you launched, while a bare pip resolves through PATH and may belong to a different environment, installing the package somewhere you did not intend.