Skip to main content

porter-sandbox

Python SDK for the Porter Sandbox API.

Pre-release. Pilot for Porter's multi-language SDK rollout.

Install

pip install porter-sandbox

Usage

from porter_sandbox import Porter

with Porter() as porter:
    sb = porter.sandboxes.create(
        image="python:3.11-alpine",
        command=["python", "-c", "print('hi')"],
    )
    print(sb.logs())
    sb.terminate()

Volumes can be created up front and mounted into sandboxes at launch, and their contents browsed and read straight from the volume handle:

volume = porter.volumes.create(name="my-data")

sb = porter.sandboxes.create(
    image="python:3.11-alpine",
    volume_mounts={"/mnt/my-data": volume.id},
)

for file in volume.listdir("/checkpoints"):
    print(file.path, file.size_bytes)

config = volume.read_text("/checkpoints/config.json")

with open("model.safetensors", "wb") as out:
    for chunk in volume.stream("/checkpoints/model.safetensors"):
        out.write(chunk)

Inside a sandbox-enabled Porter cluster, the SDK connects to the in-cluster sandbox API at http://sandbox-api.porter-sandbox-system.svc.cluster.local:8080 automatically, with no configuration needed.

From outside the cluster, set a Porter API token (created from Settings > API tokens in the Porter Dashboard) and the cluster where sandboxes are enabled:

export PORTER_SANDBOX_API_KEY=<porter-api-token>
export PORTER_CLUSTER_ID=<cluster-id>

The SDK reads the project from the token and calls the sandbox API through the Porter API at dashboard.porter.run. To target a specific URL instead, set PORTER_SANDBOX_BASE_URL or pass base_url - both take precedence over everything above.

Async

For async code (FastAPI handlers, concurrent sandbox fan-out, etc.) use AsyncPorter — same surface, awaitable methods:

import asyncio
from porter_sandbox import AsyncPorter

async def main():
    async with AsyncPorter() as porter:
        sb = await porter.sandboxes.create(image="python:3.11-alpine", command=["python", "-c", "print('hi')"])
        print(await sb.logs())
        await sb.terminate()

asyncio.run(main())

Async is also the right choice when launching many sandboxes in parallel:

async with AsyncPorter() as porter:
    results = await asyncio.gather(*[
        porter.sandboxes.create(image="python:3.11", command=cmd) for cmd in commands
    ])

Layout

Everything under porter_sandbox/ is generated from the Porter Sandbox OpenAPI spec. Do not edit it by hand - changes there are overwritten on the next release.

  • porter_sandbox/sandbox.py, porter_sandbox/volume.py - sync and async Sandbox and Volume handles
  • porter_sandbox/porter.py and resource namespace modules like porter_sandbox/sandboxes.py - public client and namespaces
  • porter_sandbox/_client.py, _models.py, enums.py, _errors.py, resources/ - low-level client, models, and errors
  • porter_sandbox/_base_client.py, _async_base_client.py, _config.py, _retries.py - sync and async HTTP transports, env-var resolution, retry/backoff

Development

pip install -e ".[dev]"
pytest
ruff check .
mypy

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

porter_sandbox-0.1.47.tar.gz (68.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

porter_sandbox-0.1.47-py3-none-any.whl (35.1 kB view details)

Uploaded Python 3

File details

Details for the file porter_sandbox-0.1.47.tar.gz.

File metadata

  • Download URL: porter_sandbox-0.1.47.tar.gz
  • Upload date:
  • Size: 68.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for porter_sandbox-0.1.47.tar.gz
Algorithm Hash digest
SHA256 e0c7a00b0ba0c183158fb4b177912445dd4120e4daae44cbf41bf405ab04c0f8
MD5 361038133f4910fc7c1b46152d2222a7
BLAKE2b-256 0ed74bf1215010d995e070aef27b991264482f8d66fae7a53ca6f6e49bea1f5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for porter_sandbox-0.1.47.tar.gz:

Publisher: release.yml on porter-dev/porter-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file porter_sandbox-0.1.47-py3-none-any.whl.

File metadata

  • Download URL: porter_sandbox-0.1.47-py3-none-any.whl
  • Upload date:
  • Size: 35.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for porter_sandbox-0.1.47-py3-none-any.whl
Algorithm Hash digest
SHA256 3e5a0ab15bea72ceba90d42f2212ae9045258a8cade29bc4b4548b964de2ca27
MD5 157f34c325505c711b0d761a8d22d252
BLAKE2b-256 c1544954b8027d90a5348cfad9fafbf1ee3938efae9825e92cb5d8f26374e191

See more details on using hashes here.

Provenance

The following attestation bundles were made for porter_sandbox-0.1.47-py3-none-any.whl:

Publisher: release.yml on porter-dev/porter-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.1.55

2 files

This release

0.1.47 This release

2 files

0.1.41

2 files

0.1.35

2 files

0.1.32

2 files

0.1.26

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page