
# Python apps

Agent Cloud builds a Python app without a Dockerfile when the repo is a uv project: a root `pyproject.toml` whose `[project]` table declares `dependencies`. Commit `uv.lock`, set `[service.web] command = "uvicorn app.main:app --host 0.0.0.0 --port $PORT"` (adjusted to your module), then `agc ship`. Success is `"outcome": "live"` with exit 0.

## Ask your agent

```text
Start a Python app on Agent Cloud with a FastAPI server and a React front end,
using the Agent Cloud Python starter. Get it running on my mirror.
```

For an existing FastAPI repo: "Bring this repo onto Agent Cloud and make sure it builds as a Python app."

## Steps

New app:

```bash
agc init --name camp-bookings --stack python
uv sync && npm ci
agc up --detach
agc ship
```

Existing uv project (`--bring` makes this folder's code the app. Without it, `--name` here fails with `init.not_new_folder`):

```bash
agc init --name camp-bookings --bring
uv lock
agc doctor
```

Then set the start and dev commands yourself, since `agc init` doesn't infer them for Python:

```toml
[service.web]
port    = 8080
command = "uvicorn app.main:app --host 0.0.0.0 --port $PORT"
dev     = "uv run uvicorn app.main:app --reload --host 0.0.0.0 --port $PORT"
health  = "/api/health"
```

Commit `uv.lock`, then `agc up --detach`, `agc check` and `agc ship`.

## How it works

**Detection.** The image comes from, in order: `[service.web] build` if it names a Dockerfile; a root `Dockerfile`; the Python default for a uv project; else the Node default. A Node app with a tool-only `pyproject.toml` (ruff or black settings, no `[project]` dependencies) stays Node. A uv project that also has a `package.json` is a Python app whose `package.json` is its UI.

**The default image.**

1. If `package.json` exists, a `node:24.22.1-slim` stage runs `npm ci` (or `npm install` without a lockfile) and `npm run build --if-present`, and keeps only `web/dist`.
2. The final stage is `python:3.12-slim` with uv from `ghcr.io/astral-sh/uv:0.13.4`. It runs `uv sync --frozen --no-dev --no-install-project`, copies the source, then `uv sync --frozen --no-dev`.
3. It sets `UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy UV_PYTHON_DOWNLOADS=never PYTHONUNBUFFERED=1 PGSSLROOTCERT=/etc/ssl/certs/ca-certificates.crt HOME=/tmp PATH=/app/.venv/bin:$PATH`.
4. It runs as user `1000:1000`, with your `command` under a shell wrapper that forwards SIGTERM, so uvicorn shuts down gracefully within the web pod's 30-second grace period.

`HOME=/tmp` matters because the data stack caches under the home folder (DuckDB extensions, matplotlib), and `/tmp` is the only place the app may write. Anything written there is gone at the next restart or release; keep the app's state, bytes it owns included, in Postgres ([Your app's Postgres](/docs/data/your-apps-postgres.md)).

**The database CA.** `DATABASE_URL` carries `sslmode=verify-full`. psycopg 3's binary wheels bundle an OpenSSL that looks for system roots in their build machine's paths, so `PGSSLROOTCERT=system` finds nothing. Agent Cloud sets `PGSSLROOTCERT` to a CA file instead. In production and rehearsals that's Debian's bundle in the default image. For `agc up` and `agc exec` it's `ca.pem` in agc's home (`~/.agentcloud`, or `$AGC_HOME`). It's set only for uv projects. With your own Dockerfile, set it there.

**On your mirror.** `agc up` and `agc doctor` check that `uv` is on `PATH` first. Dependencies are installed outside the mirror's sandbox from wheels only: `uv sync --frozen --no-install-project --no-build`, with a Python uv finds for your `requires-python`. The project itself, if it's packaged (it has a `[build-system]` and `[tool.uv] package` isn't `false`), is built inside the sandbox, offline. A `.venv` your repo tracks is refused. See [Dependencies and install scripts](/docs/build/env-mirror-and-shell-environment.md#dependencies-and-install-scripts).

**What never ships.** `.venv/`, `venv/` and `__pycache__/` are left out of every snapshot. The image installs from `uv.lock`.

## Errors

Before the build:

| code | exit | cause | fix |
|---|---|---|---|
| `ship.no_uv_lock` | 1 | A uv project with no `uv.lock` at the root | `uv lock`, commit `uv.lock`, ship again |
| `ship.no_command` | 1 | No `[service.web] command`; there's no `npm start` to fall back on | Set `command`, e.g. `"uvicorn app.main:app --host 0.0.0.0 --port $PORT"` |
| `mirror.uv_missing` | 2 | `agc up` on a uv project with no `uv` on `PATH` | `curl -LsSf https://astral.sh/uv/install.sh \| sh`, or `brew install uv` |

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "ship",
  "error": {
    "retryable": false,
    "code": "ship.no_uv_lock",
    "message": "This is a Python app (pyproject.toml declares dependencies) with no uv.lock, so its dependencies can’t be installed the same way twice.",
    "fix": "Run `uv lock`, commit uv.lock, and ship again."
  },
  "next": [{ "cmd": "agc status", "why": "see the current state before retrying" }]
}
```

When the app doesn't start, the ship fails with `ship.rehearsal_failed` or `ship.release_failed` (exit 1) and a `cause`, read from the app's own log:

| cause | What the log showed | fix |
|---|---|---|
| `module_missing` | `ModuleNotFoundError: No module named 'x'` | Add it to `pyproject.toml` (`uv add <package>`), commit `uv.lock`; for your own module, name it from the app root in `command` |
| `env_missing` / `env_given` | A `KeyError` reading `os.environ` or `getenv` | Default it with `os.environ.get`, or read the variable Agent Cloud sets at start-up |
| `loopback` | `Uvicorn running on http://127.0.0.1:…` | `--host 0.0.0.0 --port $PORT` |
| `wrong_port` | uvicorn on a port other than `[service.web] port` | `--port $PORT` |
| `port_in_use` | `[Errno 98]` / address already in use | Start one server, on `$PORT` |
| `syntax_error` | `SyntaxError`, `IndentationError`, `TabError` in `/app/…` | Fix it under Python 3.12 |
| `db_tls` | libpq's root certificate or `certificate verify failed` | Connect with `DATABASE_URL` as given; with your own Dockerfile, set `PGSSLROOTCERT` to a CA file |

## Limits

| What | Value |
|---|---|
| Python in the default image | 3.12 only |
| uv in the default image | 0.13.4 |
| Web shutdown grace | 30 s |

## Agent notes

- `uvicorn`'s default host is `127.0.0.1`. Always pass `--host 0.0.0.0` in `command`.
- Don't commit `.venv`. A tracked `.venv` makes `agc up` refuse to install.
- Keep `requires-python` within 3.12 (the starter uses `">=3.12,<3.13"`), so your mirror and the image run the same Python.
- Don't set your own host, user, password or `sslmode` for the database; use `DATABASE_URL` exactly.
- A Poetry, pip-tools or `requirements.txt` project gets the Python image once it's a uv project: declare its packages in `pyproject.toml`'s `[project] dependencies` (`uv add <package>`), then `uv lock`. For another Python version, commit a Dockerfile.

## Related

- [Starters](/docs/build/recommended-stack.md#starters)
- [How your image is built](/docs/build/how-your-image-is-built.md)
- [Dependencies and install scripts](/docs/build/env-mirror-and-shell-environment.md#dependencies-and-install-scripts)
- [Your app's Postgres](/docs/data/your-apps-postgres.md)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "build/python-apps: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
