Lecture 23 - Dependency Management, Virtual Environments, and Containers
WHERE filters rows, GROUP BY forms groups, HAVING filters groupsCOALESCE replaces it with another valueINNER JOIN keeps matches, LEFT JOIN every left row, FULL OUTER JOIN all rowsSource: Lecture 22, Appendix 07
1. Dependency management
2. Virtual environments
venv, pip and requirements.txtconda for non-Python software, uv for speed3. Containers
4. Your first image
requests, runs fast with DuckDB, and renders in Quarto-c runs a command):pip, conda) install and record them, so your code still runs laterSource: Baker (2016), Nature
conda, pip or uvrenv (R) or uv (Python)Your analysis uses, e.g., Python, NumPy, pandas and matplotlib. Ask:
pip (Python only): requirements.txtconda (Python and R): environment.ymluv (Python only): pyproject.toml and uv.lockrequirements.txt, including the project starter’svenv and pip: an isolated Pythonvenv ships with Python and creates a folder with its own python and site-packages.gitignore). Only the package list is committedwhich python shows which interpreter is active:.venv/bin/python means the environment is on. pip list shows what is installed:python3 -m venv /opt/venv, then sets ENV PATH="/opt/venv/bin:$PATH" so later steps use it without activatingrequirements.txt: the file you commitpip freeze prints every installed package with its exact version:pip also lists your dependencies’ dependencies== pins mean “these exact versions”--from-history exports only the packages you asked for:conda deactivate first, then:pip freeze can write name @ file:///... lines that fail elsewhere. Use pip list --format=freeze--from-history, it lists every package, including dependencies of dependencies:--no-builds strips build strings, but macOS-only packages stay in the file--from-historypy39h12345_0) encodes metadata and ends in the build number:
py39, py310)gcc9, clang)nomkl, cuda)linux-64, osx-arm64) is stored separatelylibblas=3.11.0=9_h51639a9_openblas from the previous slide will not resolve on Linuxuv: a fast Python package manageruv init creates a project folder with a pyproject.toml, a main.py and a git repository:uv add creates .venv, installs the packages and records them in pyproject.toml and uv.lock:activate or pip freezeuv: project files and sharingpyproject.toml lists your direct dependencies, for humans to read. Mine after uv add:[sections], close to the YAML in Quarto headersuv init also writes .gitignore, .python-version, README.md and main.py, and starts a git repositoryuv.lock records the exact version and hash of every package. Mine is 182 linesuv sync:uv run runs inside the project environment, with nothing to activateuv python install 3.14 and uv python pin 3.14 manage Python versionsuvuv: handing a project to Dockeruv export writes a requirements.txt from your lock file, for Dockerfiles that expect one:uv export --format requirements-txt --no-hashes
Resolved 8 packages in 2ms
# This file was autogenerated by uv via the
# following command:
# uv export --format requirements-txt
# --no-hashes
certifi==2026.10.23
# via requests
charset-normalizer==3.5.2
# via requests
[...]
polars==2.1.0
# via l23-uv-demo
[...]
requests==2.34.2
# via l23-uv-demo
[...]--no-hashes, each line also carries a SHA-256 hash that pip verifies# via comments show which package pulled each dependency in. pip freeze has nonerequirements.txt? uv pip is a faster drop-in for pip:uv init, uv add and uv run are simplerEach cell is one combination someone has to test by hand
Sacks, barrels, and crates, each loaded differently at every port
Shipping had the same combinatorial problem
A standard box fits every ship, train, and lorry, and nobody opens it in transit
amd64 (Intel/AMD) or arm64 (Apple Silicon)Lecture 10’s four ingredients of reproducibility: code, data, environment, documentation. A container ships the environment with the code
Dockerfiledocker pull, docker push) and Linux (docker ps, docker rm)docker command is a client. A background daemon does the building and runningClient, daemon, and registry
Source: Docker documentation
podman), Colima, OrbStack (free for personal use) and Rancher Desktop all take the same docker commandsI ran every docker command on these slides through OrbStack. Docker Desktop takes the same commands
hello-worlddocker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
58dee6a49ef1: Pull complete
Digest: sha256:5dd0d3e6e255...
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be
working correctly.
[...]A daemon error means the Docker application is not running: Appendix 02
Dockerfile is a plain-text recipe that rebuilds everything a project needsDockerfile, with no extension, and commit itlectures/lecture-23/docker/requirements.txt: the packages to installhello.py: the script the container runsDockerfile, puts them together (next slide)# Start from an official Python image on Docker Hub
FROM python:3.14-slim
# Create /app inside the image and move into it
WORKDIR /app
# Copy requirements.txt from your folder to /app
# First, so editing hello.py won't reinstall
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy hello.py from your folder into /app
COPY hello.py .
# The command that runs when the container starts
CMD ["python", "hello.py"]-r installs from the file, and --no-cache-dir keeps no copy of the downloads-t tags the image datasci350-example, and . is the folder holding the Dockerfile:docker build runs the instructions in order. COPY and RUN add layers, while CMD only records the start command| Instruction | What it does | Runs when |
|---|---|---|
FROM |
Picks the base image | Build |
WORKDIR |
Sets the folder for every later step | Build |
COPY |
Brings a file from your folder into the image | Build |
RUN |
Executes a command inside the image | Build |
CMD |
Names the command the container starts with | Start |
Official Python image: https://hub.docker.com/_/python
docker build -t datasci350-example .
#5 [1/5] FROM docker.io/library/python:3.14-slim
#5 sha256:acd43e9beb 30.21MB / 30.21MB 1.8s done
#5 DONE 2.6s
#7 [2/5] WORKDIR /app
#7 DONE 0.4s
#8 [3/5] COPY requirements.txt .
#8 DONE 0.0s
#9 [4/5] RUN pip install --no-cache-dir -r
requirements.txt
#9 5.262 Successfully installed numpy-2.5.2
pandas-3.0.5 [...]
#9 DONE 5.6s
#10 [5/5] COPY hello.py .
#10 DONE 0.0s
#11 exporting to image
#11 DONE 3.2s[1/5] to [5/5] are the instructions before CMD[1/5] downloads the base image: 2.6 seconds, once per machine[4/5] installs the packages: 5.6 secondsdocker images shows the result:docker build -t datasci350-example .
#5 [1/5] FROM docker.io/library/python:3.14-slim
#5 DONE 0.0s
#6 [2/5] WORKDIR /app
#6 CACHED
#7 [3/5] COPY requirements.txt .
#7 CACHED
#8 [4/5] RUN pip install --no-cache-dir -r
requirements.txt
#8 CACHED
#9 [5/5] COPY hello.py .
#9 CACHED
#10 exporting to image
#10 DONE 0.1sCACHED, and the build takes 0.79 secondsCOPY requirements.txt sits above COPY hello.py. Editing your script rebuilds only the last layer, and pip install stays cachedCOPY . . (copy everything) at the top, any edit reinstalls every packageDockerfile uses the same orderdocker run starts a container from the image and runs its CMD:
--rm deletes the container when it stopsThese versions come from inside the image, so every machine prints the same
You can now hand the image to someone 🤓
Log in to Docker Hub:
buildx builds for Intel and Apple chips at once, so anyone can pull itdocker pull, then run it:amd64 (Intel/AMD) and arm64 (Apple Silicon)docker/ folder from the course repository: https://github.com/danilofreire/datasci350/tree/main/lectures/lecture-23/dockerdocker build -t datasci350-example .docker run --rm datasci350-example. Three lines appearrequirements.txt. Add the line polars==2.1.0hello.py. Import polars and print pl.__version__docker build -t datasci350-example . againCACHEDdocker run --rm datasci350-example. Four lines nowCOPY lines and rebuildSolution: Appendix 01
print script)pip freeze writes == pins, and pip install -r reads them--from-history to work on other platformspyproject.toml and uv.lock, then uv sync rebuilds the environmentYour container is the deliverable: if docker build and docker run show your report on my machine, you’re done 🚀
Dockerfile, with Python and QuartoDockerfile is a small version of itBefore then:
docker run hello-worlddocker build once on your own wifiQuiz 05 covers Lectures 21, 22, 23 and 25. The final project is due on the last day of classes
Steps 5 and 6, the two edited files:
Step 9, the run:
Steps 7 and 8, the rebuild on my laptop:
#7 forced every later layer to run againpip install reran in full (8.1 seconds), downloading NumPy and pandas again along with polarsCOPY lines swapped, editing hello.py alone invalidates COPY hello.py and re-runs pip install for nothingCannot connect to the Docker daemon
Docker Desktop is not running. Start the application, wait for the whale icon to settle, and try again. On my laptop OrbStack was stopped, and docker info answered failed to connect to the docker API at unix://~/.orbstack/run/docker.sock.
permission denied ... /var/run/docker.sock
On Linux, your user is not in the docker group. Run sudo usermod -aG docker $USER, then log out and back in.
pip: command not found
Your virtual environment is not active. Run source .venv/bin/activate and check with which python, which should print a path ending in .venv/bin/python.
No matching distribution found for numpy==2.5.2
That version does not exist for your Python. Check the pin against PyPI, or loosen it to numpy>=2.0.
toomanyrequests: You have reached your pull rate limit
Docker Hub allows 100 anonymous pulls every six hours from one address. Run docker login with your free account (200 pulls) and pull again.
failed to read dockerfile: open Dockerfile: no such file or directory
You are in the wrong folder. The . at the end of docker build -t name . is where Docker looks, so cd into the folder holding the Dockerfile
With Docker Desktop (recommended)
wsl --install and restart. You get Ubuntu by default~/...), not in /mnt/c/.... Builds are much faster thereWithout Docker Desktop
Install Docker Engine inside Ubuntu, then let your user run docker without sudo:
Close and reopen the terminal. If docker cannot connect, start the engine with sudo service docker start. Use one route only, not both.
The command 'docker' could not be found in this WSL 2 distro
WSL Integration is off for your distribution. Turn it on as in step 3