Foundations to Frontier
Tests
C. Programming, Tools and HPC › C1 Python for Science

Packaging and Environments: uv, conda and pip

Reproducible research needs every machine to run the same code with the same libraries. This topic explains virtual environments (isolated site-packages per project), how pip and uv install packages from PyPI (wheels vs source distributions), how conda/mamba manage non-Python dependencies such as CUDA and compilers, how to declare a project in pyproject.toml, how version specifiers work (PEP 440: >=, ~=, ==X.*, pre-releases), why dependency resolution is a search problem, and why lock files matter. In the exercises you implement PEP 440 version ordering and specifier matching, then a backtracking dependency resolver that finds a consistent set where a naive installer produces a broken environment.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

“It works on my machine”

Your code uses libraries, those libraries use other libraries, and each comes in many versions. If you and a teammate have different versions, the same script can give different results, or not run at all. Python’s answer has three parts: a separate environment per project (its own private set of libraries), a project file listing what the project needs, and a lock file recording the exact versions that were chosen, so anyone can rebuild the identical environment.

Why is pip install sometimes slow? Watch a resolver backtrack
package index
app: 1.0 (sim>=2.0, viz>=1.0)
sim: 2.0 (numlib>=1.0,<2) · 2.1 (numlib>=2.0) · 2.2 (numlib>=2.1)
viz: 1.0 (numlib<2) · 1.1 (numlib<2.1) · 1.2 (numlib>=2.0,<2.1)
numlib: 1.5 · 1.9 · 2.0 · 2.1
your requirements: app>=1.0 plus
▸ try app 1.0 → needs sim>=2.0, viz>=1.0
▸ try sim 2.2 → needs numlib>=2.1
▸ try viz 1.2 → needs numlib>=2.0,<2.1
✗ no version of numlib satisfies >=2.1 & >=2.0,<2.1
✗ backtrack: drop viz 1.2
▸ try viz 1.1 → needs numlib<2.1
✗ no version of numlib satisfies >=2.1 & <2.1
✗ backtrack: drop viz 1.1
▸ try viz 1.0 → needs numlib<2
✗ no version of numlib satisfies >=2.1 & <2
✗ backtrack: drop viz 1.0
✗ backtrack: drop sim 2.2
▸ try sim 2.1 → needs numlib>=2.0
▸ try viz 1.2 → needs numlib>=2.0,<2.1
▸ try numlib 2.0
✅ resolved: app==1.0, sim==2.1, viz==1.2, numlib==2.0 · 8 candidates tried, 4 backtracks
The newest sim (2.2) wants numlib ≥ 2.1, but every viz that accepts numlib 2.x wants it below 2.1, so the resolver must back off to sim 2.1. A naive "install the newest of each, in order" approach would silently produce a broken environment instead. Real resolvers (pip's resolvelib, uv's PubGrub) do the same search over thousands of packages, with cleverer pruning. Lock files (uv.lock, poetry.lock, pip-tools) record the result so everyone gets the identical environment.
Why engineers care

Flight-dynamics results, trained perception models and test reports must be reproducible months later on a different computer, an HPC cluster or a CI runner. Pinned, locked environments (plus containers, C4) are what make “rerun the analysis from the paper” possible, and they are mandatory for certified ground software.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A virtual environment gives a project…
  2. 2. A lock file records…
  3. 3. Without isolation, two projects needing different versions of numpy…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Environments

python -m venv .venv && source .venv/bin/activate      # stdlib venv
uv venv && uv pip install numpy                         # uv: fast Rust-based tool
conda create -n sim python=3.12 numpy scipy && conda activate sim

A venv is just a directory with its own bin/python (Scripts\python.exe on Windows) and site-packages. Activation puts it first on PATH.

Installing

pip install "numpy>=1.24,<3" downloads from PyPI. It prefers wheels (pre-built .whl archives, platform-tagged like cp312-manylinux_x86_64) and falls back to sdists (source, built locally, which may need compilers). conda installs binaries of anything (Python, CUDA, GDAL, compilers) from its own channels (conda-forge). Use it when non-Python libraries matter, and avoid mixing pip and conda in one environment except at the end.

Declaring a project: pyproject.toml

[project]
name = "orbitkit"
version = "0.3.1"
requires-python = ">=3.10"
dependencies = ["numpy>=1.24", "scipy~=1.11"]

[project.optional-dependencies]
viz = ["matplotlib>=3.8"]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
📝 Quick check (Level 1)
0/3 answered
  1. 1. A wheel is…
  2. 2. Where does a venv's interpreter live on Linux/macOS?
  3. 3. Which tool can install CUDA toolkits and compilers as packages?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Version specifiers (PEP 440)

  • Ordering: 1.0.dev1 < 1.0a1 < 1.0b1 < 1.0rc1 < 1.0 < 1.0.post1 < 1.1 < 1.10. Components compare as integers, so 1.10 > 1.9, and 1.0 == 1.0.0.
  • Operators: ==, !=, <, <=, >, >=, wildcards ==1.2.*, and the compatible-release operator ~=1.4.2 (meaning >=1.4.2, <1.5; ~=2.3 means >=2.3, <3).
  • Pre-releases are excluded unless a specifier mentions one or you pass --pre.

Each package version declares requirements, and choices interact: picking the newest X may force a Y that conflicts with Z. pip’s resolver (resolvelib) and uv’s (PubGrub) search for an assignment satisfying every constraint, backtracking on conflicts. In general this is NP-hard (it can encode SAT), but real dependency graphs are usually easy.

Locking

uv lock, poetry lock and pip-compile record the full resolved set, with hashes, so uv sync or pip install --require-hashes -r requirements.txt reproduces it exactly, and a tampered download fails the hash check.

🐍 Exercise 1: PEP 440 versions and specifiers
Implement a sort key for PEP 440 versions (release, a/b/rc pre-releases, .post, .dev; trailing zeros ignored) and a specifier matcher supporting ==, !=, <, <=, >, >=, ~= and ==X.*, with pre-releases excluded unless requested. Then pick the newest matching version from a list.
📝 Quick check (Level 2)
0/3 answered
  1. 1. ~=1.4.2 is equivalent to…
  2. 2. Does 2.0rc1 satisfy >=1.0 by default?
  3. 3. Which is newer: 1.10 or 1.9?

Level 3: Advanced

★★★★☆ Advanced⏱ 35 minGraduate level, with full derivations and proofs.

Why resolution can be hard

Encode a SAT formula: each variable becomes a package with versions 0 and 1, and each clause becomes a package whose versions each require one literal’s choice. A consistent install exists exactly when the formula is satisfiable, so general dependency resolution is NP-complete (Di Cosmo et al., 2006). Practical resolvers rely on heuristics: newest-first, learning which conflicts to avoid (PubGrub’s incompatibility derivation, similar to CDCL SAT solvers), and pruning.

Why environments break

  • Diamond dependencies: A needs C below 2 and B needs C at least 2, with no solution.
  • Unpinned transitive dependencies drift over time (“it worked last month”).
  • ABI mismatches: compiled extensions built against a different NumPy or CUDA.
  • Mixing installers: conda and pip overwrite each other’s files.

Reproducibility ladder

Unpinned requirements.txt → pinned versions → locked with hashes → locked plus a container image (C4) → plus a fixed hardware/driver stack. Each rung removes a source of drift.

📝 Quick check (Level 3)
0/3 answered
  1. 1. General dependency resolution is…
  2. 2. A diamond-dependency conflict arises when…
  3. 3. Hashes in a lock file protect against…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Resolution, measured

Exercise 2 (toy index: app → sim, viz → numlib). Installing “the newest of everything, in order” picks sim 2.2 (wants numlib ≥ 2.1) and viz 1.2 (wants numlib < 2.1), producing an environment with an unmet requirement. The backtracking resolver tries 8 candidates, backtracks 4 times, and returns app 1.0, sim 2.1, viz 1.2, numlib 2.0, which satisfies every constraint. Adding numlib>=2.1 makes the problem unsatisfiable; the resolver proves it after exhausting 11 candidates, which is exactly the “ResolutionImpossible” error pip prints.

🐍 Exercise 2 (research): a backtracking dependency resolver
Using your specifier matcher, implement a resolver that picks the newest version satisfying all collected constraints, recurses into its requirements and backtracks on conflict. Compare it with a greedy newest-first installer, count candidates and backtracks, and detect an unsatisfiable request.

In AI, robotics and aerospace

  • Lock files and containers for reproducible training runs and flight-dynamics analyses.
  • conda-forge / mamba for GPU stacks (CUDA, cuDNN), ROS dependencies and geospatial libraries.
  • uv for fast, lockfile-based workflows in CI.
  • Internal package indexes and hash pinning for supply-chain security in mission software.

References

  1. PEP 440 (version identification), PEP 508 (dependency specifiers), PEP 517/518/621 (pyproject.toml), PEP 427 (wheels).
  2. Python Packaging Authority. Python Packaging User Guide (packaging.python.org).
  3. Astral. uv documentation (docs.astral.sh/uv).
  4. R. Di Cosmo, B. Durak, X. Leroy, F. Mancinelli, J. Vouillon (2006). “Maintaining large software distributions: new challenges from the FOSS era.” FRCSS workshop.
  5. N. Weizenbaum (2018). “PubGrub: next-generation version solving.” (medium.com/@nex3).