Back to template gallery

Python project managed by uv

A general-purpose Python Actor with its project and dependencies managed by the uv package manager. A minimal starting point for any use case.

Language

python

Tools

uv

Use cases

Starter

my_actor/main.py

pyproject.toml

my_actor/__main__.py

1"""Module defines the main entry point for the Apify Actor.
2
3Feel free to modify this file to suit your specific needs.
4
5To build Apify Actors, utilize the Apify SDK toolkit, read more at the official documentation:
6https://docs.apify.com/sdk/python
7"""
8
9from __future__ import annotations
10
11import sys
12
13from apify import Actor
14
15
16async def main() -> None:
17 """Define a main entry point for the Apify Actor.
18
19 This coroutine is executed using `asyncio.run()`, so it must remain an asynchronous function for proper execution.
20 Asynchronous execution is required for communication with the Apify platform.
21
22 This is a minimal, general-purpose Actor: it reads an input, does a little work with it, logs its progress, and
23 stores a result in the dataset. Replace the body with whatever your Actor should do, for example a scraper, a
24 browser automation, an AI agent, an MCP server, or a web server.
25 """
26 async with Actor:
27 # uv manages both the dependencies and the Python version, so the interpreter running here is exactly the
28 # one pinned in `.python-version`.
29 python_version = '.'.join(map(str, sys.version_info[:3]))
30 Actor.log.info(f'Hello from a uv-managed Apify Actor, running on Python {python_version}!')
31
32 # Retrieve the Actor input. The structure of the input is defined in `input_schema.json`.
33 actor_input = await Actor.get_input() or {}
34 name = actor_input.get('name', 'world')
35 repeat = actor_input.get('repeat', 3)
36
37 # Do something with the input. Here we simply greet the given name a few times.
38 for i in range(1, repeat + 1):
39 Actor.log.info(f'[{i}/{repeat}] Hello, {name}!')
40
41 # Save a structured result to the dataset, which is a table-like storage.
42 await Actor.push_data(
43 {
44 'greeting': f'Hello, {name}!',
45 'repeated': repeat,
46 'python_version': python_version,
47 'managed_by': 'uv',
48 }
49 )

Python uv template

A general-purpose Actor  template for Python, with the project and its dependencies managed by uv  - a fast Python package and project manager. It's a minimal starting point for any kind of Actor, for example a scraper, a browser automation, an AI agent, an MCP server, a RAG pipeline, or a standby web server.

The example code reads an input, does a little work with it, logs its progress, and stores a result in a dataset . Replace the body of main() with whatever your Actor should do. The only thing this template locks in is the tooling, uv, not the use case.

Included features

  • uv  - a single fast tool that manages the project's Python version (.python-version), virtual environment (.venv), and dependencies (pyproject.toml + uv.lock)
  • Reproducible builds - the uv.lock lockfile guarantees that the Actor's Docker image is built with exactly the dependency versions you developed against
  • Apify SDK  for Python - a toolkit for building Apify Actors  in Python
  • Input schema  - define and easily validate a schema for your Actor's input
  • Dataset  - store structured data where each object stored has the same attributes

How it works

  1. Actor.get_input() reads the input defined in the input schema 
  2. The Actor logs the Python version it runs on (managed by uv) and greets the given name a few times
  3. Actor.push_data(...) stores a structured result in the dataset

This is only a placeholder so the template runs out of the box. Swap it for your own logic and add the dependencies you need with uv add.

Working with uv

Install uv  first, then use it for everyday project management:

# Install the dependencies into the .venv virtual environment. uv also downloads
# the pinned Python version from .python-version if it's not installed yet.
uv sync
# Run the Actor locally (the Apify CLI automatically uses the .venv environment).
apify run
# Add or remove a dependency (updates pyproject.toml and uv.lock).
uv add <package>
uv remove <package>
# Upgrade all dependencies to the latest versions allowed by pyproject.toml.
uv lock --upgrade && uv sync

The Actor's Dockerfile installs the dependencies with uv sync --locked --no-dev, so the image is built with exactly the versions recorded in uv.lock (skipping any development-only dependencies you add under [dependency-groups]). Commit uv.lock and .python-version whenever they change.

Resources

Already have a solution in mind?

Sign up for a free Apify account and deploy your code to the platform in just a few minutes! If you want a head start without coding it yourself, browse our Store of existing solutions.