> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tokenfactory.nebius.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python quickstart

> Create a Python project, save a filesystem change, and reuse it in a later operation.

This quickstart uses `contree-sdk` 0.3.6 and `alpine:3.19` to create a file in one operation, save the resulting filesystem, and read the file in a second operation.

**1. Set up access**

Create an API key and copy the project ID as described in [Set up access](https://docs.tokenfactory.nebius.com/sandboxes/start/set-up-access.md#create-an-api-key-and-find-the-project-id). For the Python SDK, export both values in each shell that runs the program:

```bash theme={null}
export NEBIUS_API_KEY="YOUR_API_KEY"
export NEBIUS_PROJECT_ID="YOUR_PROJECT_ID"
```

The SDK reads `NEBIUS_PROJECT_ID`. `NEBIUS_AI_PROJECT` is the CLI's profile-registration variable and does not replace it.

**2. Create a project**

Install [uv](https://docs.astral.sh/uv/getting-started/installation/) if it is not already available, then create an isolated project and install SDK 0.3.6:

```bash theme={null}
mkdir sandboxes-quickstart
cd sandboxes-quickstart
uv init --bare --python 3.12
uv add "contree-sdk==0.3.6"
```

Use Python 3.12 for this quickstart.

**3. Save and reuse a file**

Create `quickstart.py`:

```python theme={null}
import asyncio

from contree_sdk import Contree


async def main() -> None:
    client = Contree()
    base = await client.images.use("alpine:3.19", strict=True)

    # Choose persistence before the operation is submitted.
    preparation = base.run(
        shell="printf 'ready\\n' > /tmp/status.txt",
        disposable=False,
    )
    checkpoint = await preparation

    if checkpoint.exit_code != 0:
        raise RuntimeError(f"Preparation failed: {checkpoint.stderr}")
    if checkpoint.uuid is None:
        raise RuntimeError("Preparation returned no reusable image UUID")

    print(f"Saved image UUID: {checkpoint.uuid}")

    reuse = checkpoint.run(
        shell="cat /tmp/status.txt",
        disposable=True,
    )
    result = await reuse

    if result.exit_code != 0:
        raise RuntimeError(f"Reuse failed: {result.stderr}")

    print(result.stdout, end="")


if __name__ == "__main__":
    asyncio.run(main())
```

Run it locally:

```bash theme={null}
uv run python quickstart.py
```

Expected output includes an immutable image UUID followed by:

```text theme={null}
ready
```

The object returned by awaiting a run exposes both its process result (`exit_code`, `stdout`, and `stderr`) and its saved image. When the run saves an image, you can use its `uuid` and call `.run()` again, as `checkpoint` does here. A disposable result has `uuid is None` and cannot be used to start another run.

`base.run(...)` prepares the first operation. Awaiting the returned object submits the operation and waits for its result. Because `disposable=False` was selected before submission, a successful preparation can return a reusable image. The second operation sets `disposable=True`, so its filesystem changes are discarded.

**4. Inspect failures before continuing**

An accepted operation and a completed command are different outcomes. Check the process result before using its output or image:

### Command succeeded: `exit_code == 0`

Use the output and, when requested, the returned image UUID.

### Command failed: nonzero `exit_code`

Inspect stderr and fix the command or input. Follow the [result-image rules](https://docs.tokenfactory.nebius.com/sandboxes/concepts/images-checkpoints-and-branches.md#interpret-the-result-image) before reusing files from that run.

### SDK raised an exception

The operation did not produce a normal process result. Record safe diagnostic details and follow [Troubleshooting](https://docs.tokenfactory.nebius.com/sandboxes/operate/troubleshooting.md).

> [!NOTE]
>
> When an SDK 0.3.6 wait ends before a terminal result, the SDK attempts best-effort cancellation. A REST event-stream disconnect has different behavior. See [Execution lifecycle](https://docs.tokenfactory.nebius.com/sandboxes/concepts/execution-lifecycle.md).

## Continue

* [Images, checkpoints, and branches](https://docs.tokenfactory.nebius.com/sandboxes/concepts/images-checkpoints-and-branches.md)
* [Run commands and handle results](https://docs.tokenfactory.nebius.com/sandboxes/guides/run-commands-and-results.md)
* [Prepare once, reuse safely](https://docs.tokenfactory.nebius.com/sandboxes/cookbook/prepare-once-reuse-safely.md)
* [Python SDK reference](https://docs.tokenfactory.nebius.com/sandboxes/sdk/python_sdk/reference/index.md)
