Course page
Log in

You are watching a free preview. Enroll to open all 26 lessons, track progress and ask the course tutor.

Enroll for $34.99

Section 1: Automation foundations

Setting up with uv and script dependencies

Article 11 min Lesson 2 of 26 Preview

Automation scripts often need extra packages, such as openpyxl for Excel or httpx for web requests. Installing them globally causes problems over time, because different scripts need different versions. We use uv, a fast Python package and project manager, to keep things clean.

Install uv

Follow the install instructions on the uv documentation site. On macOS and Linux it is a single command in the terminal; on Windows there is a PowerShell command. Check it worked:

uv --version

Scripts with inline dependencies

Python supports a standard way of declaring a script's dependencies in a comment block at the top of the file (PEP 723). uv reads that block and creates a temporary environment with exactly those packages:

# /// script
# requires-python = ">=3.12"
# dependencies = ["httpx", "openpyxl"]
# ///

import httpx
import openpyxl

print("Ready to automate")

Run it with:

uv run fetch_report.py

The first run downloads the packages. Later runs reuse a cache and start almost instantly. You can add a dependency to the block automatically with uv add --script fetch_report.py rich.

This approach is ideal for automation. Each script is a single file that you can copy to another computer, and it brings its own requirements with it. No separate requirements file, no forgotten pip install.

A folder for your scripts

Create an automation folder with this layout:

automation/
  scripts/      one file per task
  data/         sample input files for testing
  output/       files the scripts create
  logs/

Keep test data separate from real data. When a script is ready, you point it at the real folder by changing one setting.

Running from anywhere

Later in the course we schedule scripts to run automatically. For that, always use absolute paths or paths built relative to the script file itself, never paths relative to wherever the terminal happens to be. We will use pathlib for this in the next lesson.

← → switch lessons