A virtual environment gives a project its own Python environment configuration and installed-package location.
Separate project isolation from reproducible dependency versions and check which environment a command is using.
The program works in one terminal and cannot import its dependency in another. Before reinstalling anything, identify the interpreter each terminal is using. Then separate environment isolation, allowed version ranges and a recorded dependency resolution; each answers a different reproducibility question.

Compare chardet 5.9.1 against 5.10.0 — and pick the newer one
Same code, same data, same Python — except the laptop had chardet 5.2.0
and the server's fresh install pulled 6.0.0, which changed a return value
Quarry relied on. Before any tool can pick "the newest version in range",
it has to compare two versions, and the obvious way is wrong:
print("2.10.0" > "2.9.1")Turn a version into a tuple of ints and tuple comparison does it right:
One wrinkle: 5 and 5.0.0 are the same release, but their tuples disagree
unless you drop trailing zeros first — (5, 0, 0) != (5,) even though the
versions are equal:
print((5, 0, 0) == (5,), (1, 4) < (1, 4, 0))The bug that bug like this causes is concrete: a naive picker returns the lexically largest version, not the newest one:
The filter compares versions correctly, but the pin is still wrong — it picks 5.9.1. Make pick() return the newest release in range.
Preparing this topic’s interactive investigation…
Install chardet into its own project folder
pip install chardet downloads a file and unpacks a chardet folder into
site-packages — nothing magic, just a folder on sys.path. A virtual
environment gives each project its own:
$ python -m venv .venv
$ source .venv/bin/activate # Windows: .venv\Scripts\activate
(.venv) $ which python
/home/asha/quarry/.venv/bin/python
(.venv) $ python -m pip install "chardet>=5,<6"
Successfully installed chardet-5.2.0"Activating" just puts .venv/bin first on PATH; scripts and servers
usually skip it and call .venv/bin/python -m pip ... directly. Always
install with python -m pip, never a bare pip, so there's no doubt which
Python the package goes into. Python can tell whether it's running inside
one — sys.prefix points at the environment, sys.base_prefix at the real
install, and outside a venv they're the same:
In this browser the answer is always no — there's no venv and no pip
here, so this lesson teaches the tools in prose and real terminal sessions,
and grades you on the logic those tools apply: comparing versions, reading
specifiers, resolving pins. The exercises model a restricted version grammar; they do not implement
every rule of a production package resolver.
Reproduce the environment, not just the command
“I installed it” is incomplete evidence. The install command and the running program must use the same interpreter. On a local machine, record python -c "import sys; print(sys.executable)" and python -m pip --version; compare the paths before editing imports or reinstalling packages.
A virtual environment isolates package installations by default. It is not a container: it does not isolate your filesystem, network access or operating-system libraries. Nor should you copy a .venv directory between machines and assume it is portable. Recreate it with the intended Python and dependency specification.
The numeric tuple comparator in the exercises intentionally handles simple release numbers only. Real package versions include prereleases, postreleases, development releases and epochs. 1.0rc1 is not an integer tuple, and ~=1.4.5 has a narrower upper bound than ~=1.4. Use a standards-aware parser such as packaging.version.Version and packaging.specifiers.SpecifierSet in production tooling.
Work a resolver conflict on paper: your application needs reader>=2,<3; an installed plugin requires reader>=3. There is no version satisfying both. A lock file records a resolution, but cannot make contradictory requirements compatible. You must change a supported dependency range, choose another plugin release or separate incompatible applications into environments. Do not “fix” the conflict by deleting a constraint without testing the resulting combination.
For transfer, ask a teammate to recreate the project in a new local environment from the recorded Python version and dependency files. Run the tests there. Record platform and artifact differences when they matter; version pins alone do not certify identical native libraries, build options or external services.
References: virtual-environment boundaries and version specifiers.
Pin tonight's exact chardet version
A project states what it needs as requirements — a name plus an optional specifier:
chardet>=5,<6 # 5.x only: 6.0 changed detect()'s return value
rich~=13.7 # 13.7 or newer, but still 13.x
requests>=2.28,!=2.30.0| Specifier | Means |
|---|---|
==2.31.0 |
exactly that release |
!=2.30.0 |
anything but that release |
>=5, <6 |
ordinary comparisons, as numbers |
~=13.7 |
compatible release: >=13.7 and still 13.* |
Commas mean and — >=5,<6 is one range, and names are normalised
(lower-cased, runs of -_. collapsed to one -) so Typing_Extensions and
typing-extensions are one package. Step through one version checked
against three clauses at once:
A loose requirement is a bet that the future behaves. A pin removes it —
chardet==5.2.0 — but pinning only what you import directly isn't enough,
because rich depends on markdown-it-py, which depends on mdurl. A
lock file pins the whole installed tree, produced by tools like
pip-tools, uv or Poetry: loose requirements are the input, exact pins
are the output, and the output gets committed.
Traps worth knowing
Collisions need backtracking. Quarry needs chardet>=5; the newest
quarry-report needs chardet<5. No single chardet fits both, but an
older quarry-report that accepts chardet>=4 would — finding that means
undoing a choice and trying the next one, which is what pip's resolver does
when it reports a conflict. You build a small version of that resolver in
this lesson's capstone exercise.
Separate possibilities from measurements. A new upstream release can change an unpinned resolution, but its probability depends on actual constraints, release schedules and platforms. Inspect the resolved diff instead of attaching an invented likelihood to dependency drift.
Quick practice
def normalize(name):
import re
return re.sub(r"[-_.]+", "-", name.lower())
print(normalize("Typing_Extensions") == normalize("typing-extensions"))def vkey(v):
parts = [int(p) for p in v.split(".")]
while len(parts) > 1 and parts[-1] == 0:
parts.pop()
return tuple(parts)
print(vkey("5.0.0") == vkey("5"), sorted(["5.10.0", "5.9.1", "5.2.0"], key=vkey))A comment on a requirements line isn't part of the specifier, but it's leaking into the output. Strip it before printing.
🎯 Your turn
Build resolve_pins(requirements, index), the piece of Quarry's shipment
that writes the lock. requirements are lines from a requirements file;
index maps each normalised package name to the versions that have been
released.
- Skip blank lines and everything after a
#. - Normalise names; if a package appears on several lines, every spec applies.
- Pin the highest release that satisfies all of them, comparing as numbers.
- Report packages with no acceptable release (or none at all) as unsatisfied.
resolve_pins(["chardet>=5,<6", "rich"],
{"chardet": ["4.0.0", "5.1.0", "5.2.0", "6.0.0"], "rich": ["13.9.4"]})
# → {"pins": ["chardet==5.2.0", "rich==13.9.4"], "unsatisfied": []}normalize and vkey are in the starter. satisfies(version, spec) is
yours to finish first — it is the rule every other step leans on.
Press ▶ Run and hit ✓ Check. ✅
Practice — level up
0/6 solvedSolve each in its own editor — Run to try, Check to grade. Stuck? Reveal a Hint, or the full Solution + walkthrough.
Package names are compared after normalising them: lower-case, with every run of -, _ and . turned into a single -. So Typing_Extensions, typing.extensions and typing--extensions are one package. Write normalize(name).
Write newest(versions) returning the highest of a list of release versions like "2.9.1" and "2.10.0", compared as numbers part by part — so 2.10.0 beats 2.9.1. Missing trailing parts count as zero. An empty list gives "".
Write satisfies(version, spec). spec is comma-separated clauses, each an operator (==, !=, <=, >=, <, >, ~=) and a version; every clause must hold, and an empty spec allows anything. Compare versions as numbers, with missing trailing parts counting as zero (5 == 5.0.0). ~=2.31 is the compatible release operator: >=2.31 and still 2.x; ~=1.4.5 means >=1.4.5 and still 1.4.x.
A lock file (the output of pip freeze) pins every installed package as name==version, one per line. Write check_lock(requirements, lock): requirements are loose lines like "chardet>=5,<6", lock is the lock file's lines. Ignore blank lines and comments in both, and compare names normalised. Return a sorted list of problems: "missing: <name>" for a requirement with no pin, "not pinned: <name>" for a lock line that isn't == an exact version, and "conflict: <name>==<version> vs <spec>" when a pin breaks a requirement (use the requirement's spec text exactly as written, stripped). Extra pins the requirements don't mention are fine — they are the dependencies' dependencies.
Now the real problem: packages have dependencies of their own. index maps each package to its releases, and each release to the requirement lines it needs: {"quarry-report": {"2.0.0": ["rich>=13"]}}. Write resolve_tree(requirements, index). Work through a queue of requirements in order. For a package not yet pinned, try its releases from newest down; pin the first one that satisfies the requirement, add its own requirements to the end of the queue, and carry on. For a package already pinned, its pin must satisfy the new requirement. When nothing works, back up and try the previous package's next-newest release. Return {"pins": sorted "name==version"}, or {"error": "ResolutionImpossible"} when no combination works. Requirement names need normalising; index keys are already normal.
Quarry ships two things: the quarry command people install to process their data, and quarry-report, a small library other programs import. A teammate wants both to declare exact pins like chardet==5.2.0. Explain why exact pins in a lock file are right for the running application, but a library should declare ranges such as chardet>=5,<6 — and what goes wrong if a library pins exactly.