Python
Install the Python SDK and learn its conventions for blocking and async usage, cleanup, and errors.
pip install dvm-runPublished 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).
Blocking and asyncio
Section titled “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.
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
Section titled “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
Section titled “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
Section titled “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.
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: raiseSending a command to a one-program sandbox raises DvmError locally before
sending a request.
Agent harness
Section titled “Agent harness”Includes a Harbor environment, dvm.harbor:DvmEnvironment
(Agent harness). Python only.
Where next
Section titled “Where next”- Sessions: run commands and transfer files.
- Python reference: every class and method.
- PyPI and source.