Skip to content

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

Terminal window
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).

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
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())

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.

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.

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.

python/examples/errors.py
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.

Includes a Harbor environment, dvm.harbor:DvmEnvironment (Agent harness). Python only.