Module 2: Python & uv#

You will:

  • Run a small NumPy program.
  • Compare Python’s venv with uv’s venv.
  • Manage dependencies inside one script or a project’s pyproject.toml.

Bring:

  • Module 1’s ~/bridge-lab.
  • Python and uv.
  • Source files stored outside .venv.

Practice roadmap#

Start where you are · Choose your target

Start with data handling at 01, solve harder problems at 02, test and refactor at 03, or make your projects reproducible with uv at 04. Every roadmap stop is free; enter where your current skills fit.

Your Python and uv practice roadmapFour free stops progress from processing data on Genepy, through ranked Codewars problems and tested Exercism programs, to reproducible Python scripts and projects with uv.01020304ProcessSolveTestReproduce
  1. 01Foundation · Process

    Genepy · practical Python ↗

    Free · Browser corrector · Account for shared solutions

    Solve Sort students ↗, then CSV and Python ↗. Sort records without changing the input; write quoted CSV and parse dates and marks into typed values. New to functions and loops? Use the site's Basics exercises first.

    Move on when: both exercises pass and you can explain sorting keys, CSV quoting and type conversion.

  2. 02Intermediate · Solve

    Codewars · ranked kata ↗

    Free account · Python browser editor

    Start with Find the odd int (6 kyu) to reason about frequencies. Implement PaginationHelper (5 kyu) ↗ with correct page boundaries. When confident, try Range Extraction (4 kyu) ↗ to compress consecutive values. Lower kyu numbers mean harder problems.

    Move on when: full tests pass at 6 and 5 kyu and you can explain your strategy for duplicates, empty collections and invalid indexes.

  3. 03Applied · Test

    Exercism · test and refactor ↗

    Free account · Browser editor or local Python · Free exercises and mentoring

    Implement Bank Account ↗ with correct balances, account states and validation errors. Then refactor Ledger ↗ in small steps while preserving its locale and currency formatting. Add a regression test and record the changes that improve readability.

    Move on when: both suites pass, invalid account operations raise the expected errors, and your refactoring preserves Ledger's output.

  4. 04Project workflow · Reproduce

    uv · scripts and projects ↗

    Free tool and guides · Terminal with Python and uv

    Put a tested solution in a new folder using uv init --vcs none. Declare its dependencies and add pytest with uv add --dev pytest. Compare the project with the single-file script workflow ↗, then copy only source, tests, pyproject.toml and uv.lock into a fresh folder.

    Target: uv sync --locked and uv run --locked pytest pass in the fresh folder. Explain declared dependencies, locked versions and the recreated .venv.

Use the lab below to compare the four environment workflows. Keep practice projects in separate folders, store source files outside .venv, and use the free Codewars and Exercism accounts to save your exercise progress.

Game mission: CodeCombatWrite the route. Watch your code move. | 3 challenges

Start here#

  1. Open Kithgard Dungeon.
  2. Choose individual play if asked, Python, and the default hero.
  3. Keep the starter equipment; create a free account if needed to save progress.
  4. Open Dungeons of Kithgard and read its goals.
  5. Type one instruction per line.
  6. Before running, point to the square each instruction should reach.

Know the screen:

  • Map: shows your hero and the route.
  • Editor: holds your instructions.
  • Run: executes the instructions from the start.

Know the syntax:

  • hero.moveRight() is case-sensitive and needs the dot and parentheses.
  • Lines beginning with # are comments.

Challenge 1: Dungeons of Kithgard#

  1. Plan a route to the gem that avoids spikes.
  2. Start with hero.moveRight().
  3. Predict the next two moves.
Walkthrough: three moves

Keep one copy of the first move, then add the other two:

hero.moveRight()
hero.moveDown()
hero.moveRight()
  1. Click Run.
  2. Check the hero collects the gem and the level reports completion.
  3. Continue to the next level.

Challenge 2: Gems in the Deep#

  1. Trace a route that collects every gem.
  2. Include two upward moves to reach the upper gem.
  3. Type your route before opening the walkthrough.
Walkthrough: follow the route
hero.moveRight()
hero.moveDown()
hero.moveUp()
hero.moveUp()
hero.moveRight()
  1. Run the code and check every goal.
  2. Replace the two upward lines with hero.moveUp(2).
  3. Predict whether the route changes, then rerun.
  • Argument: the number 2 tells this function call how far to move.

Challenge 3: Shadow Guard#

  • Goal: collect the gems and reach the exit.
  • Constraint: stay out of the guard’s sight.
  • Clue: the statue provides cover.
Walkthrough: take cover above the statue
hero.moveRight()
hero.moveUp()
hero.moveRight()
hero.moveDown()
hero.moveRight()
  1. Run the code.
  2. Check the gem, survival and exit goals pass.
  3. Explain why moving up before crossing helps.

Stuck?

  • Check spelling and ().
  • Find the first move where the hero leaves your planned route.
  • Read the game’s hint before changing several lines.
  • Level order/access can vary; use the named levels if available.
  • At a subscription screen, stop and practise sequence/arguments in the local lab below.

Keep the skill:

  1. Complete at least two available levels; aim for all three.
  2. Change one move deliberately.
  3. Explain the changed result.

Your lab#

One program, four workflows#

  1. Inside ~/bridge-lab, create four folders: venv-practice, uv-practice, script-practice and uv-project.
  2. Save this code as app.py in each folder, outside .venv:
import numpy as np

scores = np.array([10, 20, 30])
print(scores.mean())
  • import numpy as np: loads NumPy, a package for numerical work, with the short name np.
  • np.array(...): creates an array of numbers.
  • .mean(): calculates their average; expect 20.0.
2.1 · Traditional Python: venv + pip
cd ~/bridge-lab/venv-practice
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install numpy
python3 app.py
  1. Create: python3 -m venv .venv makes an isolated environment in .venv; -m means run a Python module, here venv.
  2. Activate: source .venv/bin/activate puts .venv/bin first in this shell’s PATH, where it looks for commands. python3 now selects the environment’s Python.
  3. Install: python3 -m pip install numpy uses that Python’s pip to install NumPy into the environment.
  4. Run: python3 app.py executes your file with that Python and its installed NumPy.
  • Check: the output is 20.0.
  • Before continuing: run deactivate to restore the earlier PATH; the environment remains.
  • New terminal: activate again before using this environment.
2.2 · Same workflow, faster setup: uv venv
cd ~/bridge-lab/uv-practice
uv venv
source .venv/bin/activate
uv pip install numpy
python3 app.py
  1. Create: uv venv makes .venv; notice how quickly it finishes compared with the previous workflow.
  2. Activate: the same source command updates PATH to select this environment’s Python.
  3. Install: uv pip install numpy installs NumPy into the environment; pip itself need not be installed there.
  4. Run: python3 app.py runs the same program; expect 20.0.
  • Same idea: create → activate → install → run; uv handles creation and installation.
  • Before continuing: run deactivate.
2.3 · Modern single file: dependencies travel with the script
cd ~/bridge-lab/script-practice
uv add --script app.py numpy
uv run app.py
  1. Declare: uv add --script app.py numpy adds dependency metadata at the top of the existing file. Look for numpy in its dependencies list.
  2. Run: uv run app.py prepares an isolated environment with NumPy and executes the script; no manual activation is needed.
  • Check: the output is still 20.0; your program below the metadata is unchanged.
  • Share: send app.py, including its metadata; someone with uv can run it.
  • Keep for Module 4: copy this version to ~/bridge-lab/scripts/check-in.py.
2.4 · Modern project: dependencies in pyproject.toml
cd ~/bridge-lab/uv-project
uv init --vcs none
uv add numpy
uv run app.py
  1. Initialise: uv init creates a Python project; --vcs none leaves Git setup for Module 4.
  2. Declare and install: uv add numpy records NumPy in pyproject.toml and prepares the project’s environment.
  3. Run: uv run app.py selects that environment and executes your file; expect 20.0.
ItemMeaning
pyproject.tomlProject settings and declared dependencies
uv.lockExact resolved dependency versions
.venv/Local environment uv manages
  • Check: find numpy under dependencies in pyproject.toml.
  • Share: commit source files, pyproject.toml and uv.lock; recreate .venv locally.
  • Choose a project: when several files share dependencies.

Finish without copying#

  1. Change the numbers in each app.py to [10, 20, 60].
  2. Predict the average, then rerun using each workflow.
  3. In notes/python.txt, explain what selects Python and where dependencies are declared.
WorkflowSelect the environmentDependencies
Python venv + pipActivate, then python3 app.pyInstall manually with pip
uv venvActivate, then python3 app.pyInstall manually with uv pip
uv single fileuv run app.pyMetadata inside app.py
uv projectuv run app.pypyproject.toml + uv.lock
  • I completed two or three game levels.
  • I can explain -m, activation and PATH.
  • I ran the same NumPy program in all four workflows.
  • I can choose between a standalone script and a project.

Stuck?

  • ModuleNotFoundError: NumPy must be installed in the environment that runs your file. Check activation, or use the matching uv workflow.
  • venv missing on Ubuntu/WSL: revisit Module 1’s python3-venv installation.

Explore: change .mean() to .sum(), predict the result and verify it.

References: Python venv · uv environments · uv scripts · uv projects.


Next: Module 3: HTTP & Chrome DevTools →