Skip to main content
One call, await cb.run(code), starts a python:slim sandbox, runs the code, and returns its stdout. pip install works inside the box, and everything is cleaned up on exit.

Quick Example (minimal happy path)

This runs as-is. The first run pulls the image first, which can take tens of seconds.

Install a package, then execute

Note: run() returns stdout only. If the code writes to stderr (for example, a traceback), use exec() below to get the full result.

Need both stdout and stderr / check the exit code

run() does not raise on a non-zero exit and does not return stderr. To get the error output or exit code, use exec(), which is inherited from SimpleBox:

Parameters and Returns

CodeBox(...) constructor parameters

CodeBox extends SimpleBox; other parameters (volumes, network, secrets, advanced, and so on) pass through via **kwargs. To lock a CodeBox down for untrusted code, see Harden the box for untrusted code.
CodeBox passes memory_mib / cpus straight through, so an unset value resolves to the same VM defaults as every other Box type. Compute resources → Defaults is the single source of truth for those numbers; set both explicitly in production rather than relying on them.

await cb.run(code, timeout=None) -> str

Returns: the stdout string of the execution. A non-zero exit does not raise (unlike the Node SDK’s run, which throws ExecError).

Other common methods (all async)

install_package and install_packages return result.stdout + result.stderr concatenated, because pip writes progress to both streams.

ExecResult fields (returned by exec())

Timeout naming / type reminder: SimpleBox.exec’s timeout is a float (seconds), while CodeBox.run’s timeout is an int. Use exec when you need a hard timeout.

Optional: Node SDK equivalent

Troubleshooting

run() returns no error info / an empty string

run() returns stdout only and does not raise on a non-zero exit. If the code errors, the traceback is on stderr, which run() does not see. Use exec("/usr/local/bin/python", "-c", code) instead and read result.stderr and result.exit_code (see the example above).

ModuleNotFoundError: No module named 'xxx'

python:slim ships only the standard library. A third-party library must be installed with await cb.install_package("xxx") (or install_packages(...)) before run(). Installing packages requires PyPI to be reachable.

RuntimeError (image pull failure / no virtualization)

  • A network hiccup during image pull raises a standard builtins.RuntimeError (not BoxliteError); you can retry with try/except RuntimeError.
  • Starting a sandbox requires hardware virtualization: Linux needs KVM (the user in the kvm group; same on WSL2); macOS Apple Silicon uses Hypervisor.framework and needs no /dev/kvm; macOS Intel is not supported. Without virtualization, startup fails but the process does not crash, so the exception can be caught.

RuntimeError: Box not started ...

CodeBox is an async context manager; the sandbox is actually created and started when you enter async with. Call run/exec inside the async with boxlite.CodeBox() as cb: block, or call await cb.start() first.

Limiting execution time

CodeBox.run’s timeout is not enforced. When you need an enforced timeout, use exec:
exec’s timeout is in seconds (float).