Packages and Virtual Environments

The Problem

uv

curl -LsSf https://astral.sh/uv/install.sh | sh
uv --version
uv 0.4.18

Creating a Project

uv init birdcount
cd birdcount
ls -a
.
..
.python-version
README.md
hello.py
pyproject.toml

pyproject.toml

[project]
name = "birdcount"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []

Adding Dependencies

uv add requests
Resolved 5 packages in 234ms
Prepared 5 packages in 1.23s
Installed 5 packages in 89ms
 + certifi==2024.8.30
 + charset-normalizer==3.3.2
 + idna==3.10
 + requests==2.32.3
 + urllib3==2.2.3
[project]
name = "birdcount"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
    "requests>=2.32.3",
]

The Lock File

Running Code

import requests

response = requests.get("https://example.com/birds.json")
print(f"status: {response.status_code}")
uv run python fetch_birds.py
status: 200

How the Virtual Environment Works

uv run python -c "import sys; print(sys.prefix)"
/Users/tut/birdcount/.venv

Reproducing an Environment

uv sync
Resolved 5 packages in 12ms
Installed 5 packages in 341ms
 + certifi==2024.8.30
 + charset-normalizer==3.3.2
 + idna==3.10
 + requests==2.32.3
 + urllib3==2.2.3

Removing and Upgrading Dependencies

Exercise: First Project

  1. Create a new project called weather using uv init.

  2. Add httpx as a dependency and write a script that fetches any URL and prints its status code.

  3. Open uv.lock and find httpx. How many packages were installed to satisfy that one dependency?

  4. What is in .venv/bin? Why is there more than just Python there?