# Python

> Install the dvm-run Python package (3.12+), choose blocking or asyncio, and manage cleanup and errors.

This is the Markdown of https://docs.dvm.run/sdks/python/. Every page of the dvm.run docs is served as Markdown at its address plus `.md`; the list of pages is https://docs.dvm.run/llms.txt.

Install the Python SDK and learn its conventions for blocking and async usage, cleanup, and errors.



```sh
pip install dvm-run
```

Published as `dvm-run` on PyPI and imported as `dvm`. Requires Python 3.12 or newer and depends only on `httpx`. Includes the `dvm-run` CLI, also runnable via `python -m dvm` ([Getting in](/start/access/)).

## Blocking and asyncio

`Sandbox` and `SandboxPoint` block; `AsyncSandbox` and `AsyncSandboxPoint` provide the same methods as asyncio coroutines. `Command` and `AsyncCommand` are the corresponding handles for started commands.

`python/examples/asyncio_sandbox.py`

```python
async def main() -> None:
    async with await AsyncSandbox.create("python:3.12") as sandbox:
        results = await asyncio.gather(
            sandbox.run(["python", "-c", "print(1)"]),
            sandbox.run(["python", "-c", "print(2)"]),
        )
        print([result.stdout for result in results])
        await sandbox.end()
        point = await sandbox.seek()
        print(point.event)


asyncio.run(main())
```

## Create and end a sandbox

`Sandbox.create(image, ...)` returns once the sandbox runs (or has exited). Keyword arguments: `command`, `entrypoint`, `cpus`, `memory_mib`, `seed`, `env`, `name`, `network`, `allow`, `api_key`, and `api_url`.

Use a `with` block (`async with` for `AsyncSandbox`). When the block exits:

* a session (a sandbox accepting commands) closes its input, and the block waits for exit;
* a paused sandbox or one-program sandbox is stopped;
* an ended sandbox is left unchanged.

The handle then closes its connection. Outside a `with` block, call `end()` or `stop()`, then `close()`. `close()` alone leaves the sandbox running.

Call `Sandbox.connect(sandbox_id)` to attach to a sandbox created elsewhere.

## Bytes and results

`run` and `Command.wait()` return a `CommandResult` with `exit_code`, `stdout`, and `stderr` (`bytes`). File contents are also `bytes`. A command’s exit code is never an error.

## Errors

All exceptions inherit from `DvmError`. HTTP errors raise `ApiError` (`status`, `message`), with subclasses for 400, 401, 404, and 409. Calling an ended sandbox raises `SandboxEndedError`, or `SandboxFailedError` (with `reason`) if dvm or the platform ended it. Sandbox-side rejections, such as a missing file, raise plain `DvmError` with the message. See [Errors](/reference/errors/).

`python/examples/errors.py`

```python
try:
    with Sandbox.create("alpine:3.20", command=["true"], cpus=64) as sandbox:
        sandbox.wait()
except InvalidRequestError as refused:
    print(refused.status, refused.message)
except SandboxFailedError as failed:
    print("dvm ended it:", failed.reason)
except ApiError as refused:
    if refused.status == 402:
        print("out of credit:", refused.message)
    elif refused.status == 403:
        print("not let in yet:", refused.message)
    else:
        raise
```

Sending a command to a one-program sandbox raises `DvmError` locally before sending a request.

## Agent harness

Includes a Harbor environment, `dvm.harbor:DvmEnvironment` ([Agent harness](/guides/harbor/)). Python only.

## Where next

* [Sessions](/guides/sessions/): run commands and transfer files.
* [Python reference](/reference/python/): every class and method.
* [PyPI](https://pypi.org/project/dvm-run/) and [source](https://github.com/deterministic-machines/sdk/tree/main/python).
