Python, properly · Chapter 20 of 42

uv

By the end of this chapter you can create a Python project with uv, add packages to it, run code inside its own virtual environment, and explain what pyproject.toml, uv.lock and .venv are for. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Code that holds up
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

Farah wants the menu posted to the WhatsApp group as a neat table, and Bhavna's dates come typed in every style. Other people have already solved both problems: tabulate draws tables, python-dateutil reads almost any date. They are not part of Python, so you have to install them.

Installing raises real questions. Where do packages go? What happens when one project needs version 1 of a package and another needs version 2? How does Kunal's laptop, or the server, get exactly the packages your code ran with? uv answers all three: one fast tool that creates the project, keeps its packages apart, and writes down exactly what was installed.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    main.py: a uv project in this folder

    In the terminal, in this chapter's folder, turn the folder into a uv project of scripts (not a package to publish) named dabba-tools: uv init with its no-package option and its name option. It writes pyproject.toml and keeps this main.py.

    Then make main.py print exactly one line, dabba-tools is ready, and run it the uv way: uv run main.py

    Leave uv.toml where it is: it points uv at /opt/wheels, because the lab has no internet.

  2. Task 2

    menu_table.py: a table with tabulate

    Farah wants the veg menu as a table for WhatsApp. Add the tabulate package to the project with uv add, then in menu_table.py read /workspace/data/menu.json and print what tabulate(rows, headers=["Dish", "Price"]) returns, and nothing else.

    rows holds one [name, price] list for each dish whose category is veg and whose available is true, in the menu's order.

    The check looks for tabulate in pyproject.toml and uv.lock, and runs uv run menu_table.py.

  3. Task 3

    when.py: a second dependency

    Bhavna's WhatsApp dates come in every style. Add python-dateutil to the project with uv add. Its module dateutil.parser has a function parse(text): it reads a date written in almost any style and returns a datetime, whose .date() prints as 2026-09-05.

    when.py prints Bhavna's four dates, one per line, as dates: 5 Sep 2026, 03-09-2026, 12/09/2026 and 14/09/2026 7:30 PM become 2026-09-05, 2026-09-03, 2026-09-12 and 2026-09-14.

    Careful: she writes the day first, the Indian way, but parse reads 03-09-2026 as the 9th of March unless you give it the option dayfirst=True.

  4. Task 4 · interview question

    Interview: which_python.py, which Python is running?

    Print exactly two lines: prefix: followed by a space and sys.prefix, the folder of the Python environment running the file; then tabulate: yes if the module tabulate can be imported, or tabulate: no if it cannot.

    The check runs it both ways: python3 which_python.py (the computer's Python: its own prefix, and tabulate: no) and uv run which_python.py (this project's .venv as the prefix, and tabulate: yes).

    Be ready to explain what a virtual environment really is, using what the two runs print.

What you will learn

  • Packages: code other people published, and where Python finds it.
  • Virtual environments: a folder with its own packages, per project.
  • uv init and pyproject.toml: a project described in one file.
  • uv add, uv.lock and uv run: dependencies added, locked to exact versions, and used.

More in Code that holds up

Build "uv" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.