← Lab 8

1 · The terminal

The same computer, driven by typing. Terminal on a Mac, PowerShell on Windows. Every other tool today starts here.

The commands
terminal
pwd              # where am I?
ls               # what is here?
mkdir bio-python # make a folder
cd bio-python    # go into it
cd ..            # one folder up
Remember
  • ▸Most errors that say a file cannot be found mean you are in the wrong folder: pwd, then ls
  • ▸Your bio-python folder from Lab 1 is where every lab lives
  • ▸Press Tab to complete a file or folder name; ↑ brings back the last command

2 · uv and Python

One tool installs Python, runs your files and, later, manages your packages. If a command is not found after installing, close and reopen the terminal.

The commands
terminal
uv python install 3.14
uv run python --version   # Python 3.14.x
uv run python             # the >>> prompt; exit() to leave
uv run translate_bee.py   # run a program
Remember
  • ▸The >>> prompt saves nothing. Real work lives in a .py file
  • ▸uv run picks the right Python for you, so you never type python3 or py
  • ▸uv self update keeps uv itself up to date

3 · VS Code, set up your way

Write and run in one window. Lab 3 set it up; Lab 6 made it yours.

The commands
VS Code
File → Open Folder        never a lone file
⌘⇧P / Ctrl+Shift+P        find any command
  → Python: Select Interpreter
⌘, / Ctrl+,               settings
Ctrl+`                    open the terminal
⌥Z / Alt+Z                wrap long lines
Remember
  • ▸Always open the folder: the Explorer, the terminal and the interpreter all follow it
  • ▸The Python extension must be the one by Microsoft
  • ▸Run with ▶ (top right) or type uv run file.py in the terminal: same result
  • ▸Settings: "files.autoSave": "afterDelay", so a script always runs what you see; "chat.disableAIFeatures": true switches off the ghost text

4 · Projects and virtual environments

Colab hid this from you: on your laptop, every package has to be installed. Each project gets its own, in its own folder.

The commands
terminal
uv tool install uv-start      # once per laptop
uv-start lab08 --data         # a new project folder
cd lab08
uv add scipy                  # add a package
uv sync                       # rebuild .venv from the lock
Remember
  • ▸pyproject.toml is the list of packages, uv.lock the exact versions, .venv/ the packages themselves
  • ▸ModuleNotFoundError means the package is not in this project yet: uv add it
  • ▸Never pip install in a uv project
  • ▸data/ goes in and is never overwritten; results/ comes out

5 · Notebooks and scripts

Two ways to write Python. Both run with the same packages, from the same .venv.

.ipynb · for data analysis
  • ▸Cells you run one at a time, with the output underneath
  • ▸In VS Code: Select Kernel → Python Environments → .venv. The kernel is the whole trick
  • ▸The trap: cells run in any order. Restart → Run All before you trust a result
.py · for programs
  • ▸Plain text, run top to bottom, every time the same
  • ▸In the terminal: uv run count_trials.py
  • ▸Functions you want to reuse across notebooks belong in a .py file
terminal
uv run count_trials.py

6 · git and GitHub

A lab notebook for code: every working state saved, with a note saying why. Commit when something works.

The commands
terminal
git status                 # when lost, type it
git add .
git commit -m "why, not what"
git log --oneline          # the history
git restore analysis.py    # back to the last commit
git push                   # copy commits to GitHub
Remember
  • ▸git init makes the hidden .git folder: one repo per project. uv-start already did it for you
  • ▸.gitignore keeps .venv/ and big data files out
  • ▸git restore throws away uncommitted work, with no undo
  • ▸Prefer buttons? The VS Code Source Control panel runs the same commands

All of it, once

Run today's two notebooks on your own laptop, as a project under git. Every step uses a tool from the last seven labs. Windows: type curl.exe instead of curl.

1 · Terminal: a project with the notebooks in it
terminal
cd bio-python
uv-start lab08 --data
cd lab08
uv add scipy
curl -O https://raw.githubusercontent.com/HocheggerLab/y3-bio-python/main/lab08/01_quicker_figures.ipynb
curl -O https://raw.githubusercontent.com/HocheggerLab/y3-bio-python/main/lab08/02_real_or_chance.ipynb
code .
2 · VS Code, then git
  • ▸Open 01_quicker_figures.ipynb, Select Kernel → .venv, run the first cell. It downloads depmap.csv
  • ▸Keep the 2 MB table out of git: add a line depmap.csv to .gitignore
  • ▸In the terminal: git status, git add ., git commit -m "lab 8 notebooks run"
  • ▸Optional: make an empty lab08 repo on GitHub and push to it, as in Lab 7

Done when the notebook runs in VS Code and git log --oneline shows your commit.

When it breaks

Seven messages cover most of what goes wrong. Read the last line of the error first.

command not found: uvClose and reopen the terminal. Still missing: uv tool update-shell
code: command not foundIn VS Code, ⌘⇧P → Shell Command: Install 'code' command in PATH
No such file or directoryWrong folder. pwd, ls, then cd to the right one
ModuleNotFoundErroruv add the package, or pick the .venv kernel
VS Code asks to install ipykernelYou picked the wrong Python. Select Kernel → .venv
Something is odd after uv adduv sync, then ⌘⇧P → Developer: Reload Window
fatal: not a git repositoryYou are outside the project folder. cd lab08