Python and tooling for ML work

A reproducible environment, typed async Python, notebooks that don't rot, and GPUs you can rent by the hour.

Beginner about 4 h

The picture

  1. 01

    uv

    env + lockfile

  2. 02

    Type hints

    catch bugs early

  3. 03

    async/await

    concurrent API calls

  4. 04

    Notebook → script

    reproducible runs

  5. 05

    GPU access

    Colab, cloud

Why it matters

Most "it works on my machine" failures in AI projects are environment failures: an unpinned library, a notebook cell run out of order, a key hard-coded in a cell. Fix this once and everything later gets cheaper.

A project that reproduces

curl -LsSf https://astral.sh/uv/install.sh | sh
uv init llm-lab && cd llm-lab
uv add numpy torch pydantic httpx python-dotenv
uv add --dev jupyter ruff pytest
uv run python -c "import torch; print(torch.__version__, torch.cuda.is_available())"

uv.lock pins every transitive dependency. Commit it. Keep secrets in .env, add .env to .gitignore, and load them with python-dotenv.

Typed, async Python

LLM work is mostly waiting on the network. Learn asyncio early, with a semaphore so you never hammer a rate limit.

import asyncio
import httpx

async def fetch(client: httpx.AsyncClient, url: str, sem: asyncio.Semaphore) -> int:
    async with sem:
        r = await client.get(url, timeout=20)
        return r.status_code

async def main(urls: list[str], concurrency: int = 5) -> list[int]:
    sem = asyncio.Semaphore(concurrency)
    async with httpx.AsyncClient() as client:
        return await asyncio.gather(*(fetch(client, u, sem) for u in urls))

if __name__ == "__main__":
    print(asyncio.run(main(["https://example.com"] * 20)))

Add type hints everywhere and run ruff and a type checker. Pydantic models give you validated data classes, which you will lean on in the structured-output topic.

Notebooks vs scripts

Explore in a notebook, then move anything you rerun into src/ as a function with a test. A useful rule: a notebook may import your code but your code never imports a notebook.

GPUs

You do not need to own one. Colab and the major clouds rent GPUs by the hour. Always check torch.cuda.is_available() and write device-agnostic code:

device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
model = model.to(device)

Common mistakes

  • Installing into the system Python instead of a project environment.
  • Running notebook cells out of order and trusting stale state. Use Restart and run all before sharing.
  • Committing API keys. Rotate any key that has ever touched git.

Exercises

  1. Create the project above and run it on a second machine or a fresh container.
  2. Make the fetch example print the slowest URL and its latency.

You are done when you can…

  • Create a project with a pinned lockfile and run it on a clean machine
  • Write a typed async function that fans out 20 requests with a concurrency limit
  • Explain when to use a notebook and when to promote the code to a module